public partial class Window2 : Window
{
int margin = 200;
public Window2()
{
this.InitializeComponent();
for (int i = 1; i <= 5; i++)
{
TextBlock DynamicLine = new TextBlock();
DynamicLine.Name = "lbl_DynamicLine" + i;
DynamicLine.Width = 600;
DynamicLine.Height = 20;
DynamicLine.Text =i+"Dynamic TextBlock";
DynamicLine.Margin = new Thickness(50, margin, 0, 0);
margin = margin + 20;
LayoutRoot.Children.Add(DynamicLine);
}
}
}
I tried to remove the textblock dynamically like below.
LayoutRoot.Children.Remove(DynamicLine);
But i can remove the last created textblock only with above code line.Now i want to remove all textblock dynamically. what should i do for that.
try this code
public partial class Window2 : Window
{
int margin = 200;
TextBlock DynamicLine;
public Window2()
{
this.InitializeComponent();
for (int i = 1; i <= 5; i++)
{
DynamicLine = new TextBlock();
DynamicLine.Name = "lbl_DynamicLine" + i;
RegisterName(DynamicLine.Name, DynamicLine);
DynamicLine.Width = 600;
DynamicLine.Height = 20;
DynamicLine.Text =i+"Dynamic TextBlock";
DynamicLine.Margin = new Thickness(50, margin, 0, 0);
margin = margin + 20;
LayoutRoot.Children.Add(DynamicLine);
}
for (int i = 1; i <= 5; i++)
{
DynamicLine = (TextBlock)this.FindName("lbl_DynamicLine" + i);
LayoutRoot.Children.Remove(DynamicLine);
}
}
}
To delete all children, you should call the clear method.
LayoutRoot.Children.Clear();
for (int i = LayoutRoot.Children.Count; i > 0; i--)
{
LayoutRoot.Children.RemoveAt(i-1);
}
Related
I am attempting to dynamically add a list of buttons:
private void updateClientListUI()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.updateClientListUI));
}
else
{
int count = 1;
int x = 0;
int y = 0;
for (int i = 0; i < 5; i++)
{
Button btn = new Button();
btn.Text = count.ToString();
btn.Name = count.ToString();
btn.Size = new Size(35, 35);
btn.Location = new Point(150, 150 * y);
//btn.Dock = DockStyle.Fill;
y++;
count++;
Controls.Add(btn);
}
}
}
Unfortunately this does not apply any buttons to the form.
In addition I was wondering how could I append these buttons in a panel called subPanelClient
This worked for me, the issue was the position of the button, as you have to indicate it within a panel or form.
In this case i just docked them to the panel
private void updateClientListUI()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.updateClientListUI));
}
else
{
//Debug.WriteLine(clientNames[0]);
int basex = subPanelClient.Location.X;
int basey = subPanelClient.Location.Y;
subPanelClient.Controls.Clear();
Debug.WriteLine(clientNames.Count);
for (int i = 0; i < clientNames.Count; i++)
{
Button b = new Button();
b.Left = basex;
b.Top = basey;
b.Size = new Size(25, 25); // <== add this line
b.Dock = DockStyle.Top;
b.ForeColor = Color.Gainsboro;
b.FlatStyle = FlatStyle.Flat;
b.FlatAppearance.BorderSize = 0;
b.Padding = new Padding(35, 0, 0, 0);
b.TextAlign = ContentAlignment.MiddleLeft;
basey += 25;
b.Name = clientNames[i];
b.Text = clientNames[i];
subPanelClient.Controls.Add(b);
buttonsAdded.Insert(i, b);
}
}
}
I started yesterday a new proyect in c# with WPF. My first time with it.
I'm trying to do the tictactoe game with graphical interface so i create the grid and i use bottons to change the state (it's not finish yet).
Here is the declaration of the class:
public partial class juego : Window
{
private juego tab;
public juego( int size)
{
InitializeComponent();
this.tab = CreateDynamicWPFGrid(size);
}
Here is the method that created the grid.
public juego CreateDynamicWPFGrid(int size)
{
Grid DynamicGrid = new Grid();
DynamicGrid.Name = "GridTablero";
DynamicGrid.Width = 400;
DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
DynamicGrid.ShowGridLines = true;
DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);
for (int i = 0; i < size; i++)
{
ColumnDefinition gridCol1 = new ColumnDefinition();
DynamicGrid.ColumnDefinitions.Add(gridCol1);
RowDefinition gridRow1 = new RowDefinition();
gridRow1.Height = new GridLength(45);
DynamicGrid.RowDefinitions.Add(gridRow1);
}
for (int fila = 0; fila < DynamicGrid.RowDefinitions.Count; fila++)
{
for (int columna = 0; columna < DynamicGrid.RowDefinitions.Count; columna++)
{
System.Windows.Controls.Button newBtn = new Button();
newBtn.Content = fila.ToString() + "_" + columna.ToString();
newBtn.Name = "Button" + fila.ToString() + "_" + columna.ToString();
newBtn.SetValue(Grid.ColumnProperty, columna);
newBtn.SetValue(Grid.RowProperty, fila);
newBtn.Click += new RoutedEventHandler(button_Click);
DynamicGrid.Children.Add(newBtn);
}
}
tablero.Content = DynamicGrid;
return tablero;
}
So the thing is that i want to iterate over the grid and then, count the buttons which content means if they are X, O or white.
I tried to use in my private method something like tab.Content but i really don't have any idea.
Anyways, i would like to know if this it is even possible.
After all, i come up with a solution by myselft.
So, i changed some lines in the class.
public partial class juego : Window
{
private ArrayList jugadores;
public juego(ArrayList jugadores, int size)
{
InitializeComponent();
tablero.Content = CreateDynamicWPFGrid(size);
}
Then i changed the return from the CreateDynamicWPFGrid:
return dynamicGrid;
And then, one method that i call when i click on buttons.
Grid boardValidar = tablero.Content as Grid;
Button[,] botones = new Button[boardValidar.ColumnDefinitions.Count, boardValidar.ColumnDefinitions.Count];
var buttons = boardValidar.Children.Cast<Button>();
for (int i = 0; i < boardValidar.ColumnDefinitions.Count; i++)
{
for (int j = 0; j < boardValidar.RowDefinitions.Count; j++)
{
botones[i, j] = buttons.Where(x => Grid.GetRow(x) == j && Grid.GetColumn(x) == i).FirstOrDefault();
}
}
So, i cast the grid and then, i do the same with all the buttons.
After this i use the linq to put in the array and that's all.
I know my code it's no the best but i got what i wanted.
I have a Windows Forms application and I'm trying to add buttons to mimic a calculator.
public class myform : Form
{
public myform()
{
//setting size of form
this.Text = "Calculator";
this.Height = 600;
this.Width = 400;
//creating buttons from 0-9
Button[] b = new Button[10];
int x = 0;
int y = 0;
string ch;
for (int i = 0; i < b.Length; i++)
{
ch = Convert.ToString(i);
x = 0;
y = y + 50;
b[i] = new Button();
b[i].Height = 40;
b[i].Width = 40;
b[i].Text = ch;
for (int j = 0; j < 3; j++)
{
x = x + 50;
b[i].Location = new Point(x, y);
}
}
for (int i = 0; i < b.Length; i++)
{
this.Controls.Add(b[i]);
}
}
}
here is the form class in which i am creating the object of myform class described above.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
myform mf = new myform();
mf.Show();
}
}
The problem seems to be that you always set the x value to x = x + 150; I suggest you change your x value to x = (i%3) * 50; and your y to y= (i/3) *50;
that should provide you with a nice array of buttons.
ch = Convert.ToString(i);
x = (i%3)*50;
y = (i/3)*50;
b[i] = new Button();
b[i].Height = 40;
b[i].Width = 40;
b[i].Text = ch;
b[i].Location = new Point(x, y);
Will be your new loop body.
on startup I'm generating a lot of controls 90 to be exact and everything is working ok EXCEPT for the labels they are not being drawn or something? they are there because I can click them and they show proper ID (click event) here's the genereation code
private static bool ClientsLoaded = false;
private static WebBrowser[] Clients = new WebBrowser[45];
private static Label[] ClientLabel = new Label[45];
private static int MaximizedClient = -1;
public Form1()
{
InitializeComponent();
int WBoffsetX = 0;
int WBoffsetY = 0;
int lbloffsetX = 0;
int lbloffsetY = 0;
for (int i = 0; i < 45; i++)
{
var wb = new WebBrowser();
Clients[i] = wb;
wb.ScrollBarsEnabled = false;
wb.Height = 12;
wb.Width = 12;
wb.Location = new Point(2 + WBoffsetX, 2 + WBoffsetY);
WBoffsetX += 13;
wb.ScriptErrorsSuppressed = true;
this.Controls.Add(wb);
ClientLabel[i] = new Label();
ClientLabel[i].Name = "lbl_" + i;
ClientLabel[i].Font = new Font("Arial", 12);
ClientLabel[i].ForeColor = System.Drawing.Color.White;
ClientLabel[i].Location = new Point(12 + lbloffsetX, 450 + lbloffsetY);
lbloffsetX += 22;
ClientLabel[i].Click += new EventHandler(lbl_click);
ClientLabel[i].Text = "C" + i + ": o";
this.Controls.Add(ClientLabel[i]);
}
}
I've tried adding a button with for(45) clientlabel[i].Refresh() and it did nothing I tried changing the visibilty of them all to false and then back to true and nothing however I did find 1 thing interesting if I hide lbl_1 label 2 text will appear if I had label 2 label 3 text will appear but if I change the previous label back to visible they stay invisible textwise
I can click in a line on the form and
private void lbl_click(object sender, EventArgs e)
{
int id = -1;
var s = sender.ToString();
for(int i = 0; i<=45; i++)
{
if (s.Contains("C" + i + ":"))
{
id = i;
}
}
MessageBox.Show("Hello label, " + id);
}
will pop up the proper ids etc
does anyone know what's causing this maybe? or how to fix it
Well, I don't know what is the problem. This code works well enough and it has only marginal differences with the original(AutoSize property, explicit statement of Height and Width, and minor Location adjustment):
for (int i = 0; i < ClientLabel.Length; i++)
{
// Web browsers
WebBrowser wb = new WebBrowser()
{
ScrollBarsEnabled = false,
Height = 12,
Width = 12,
Location = new Point(2 + WBoffsetX, 2 + WBoffsetY),
ScriptErrorsSuppressed = true
};
WBoffsetX += 13;
Clients[i] = wb;
// Labels
Label label = new Label()
{
Name = "label_" + i,
Text = "Data",
AutoSize = true,
Location = new Point(50 + lbloffsetX, 50 + lbloffsetY),
Width = 100,
Height = 20,
Font = new Font("Arial", 12),
ForeColor = System.Drawing.Color.White,
};
label.Click += new EventHandler(lbl_click);
ClientLabel[i] = label;
lbloffsetX += 30;
}
this.Controls.AddRange(Clients);
this.Controls.AddRange(ClientLabel);
I am new to Windows app developemnt:
I have a scenario where i need to display matrix of button with text below that, i am able to do that but the issue here is the matrix can be of any thing 2x2,2x3,2x4 or 2x6.
but the button should be square and not rectangle, if i add image to it then the image looks stretched.
here is my code :
public partial class MainPage : PhoneApplicationPage
{
int numberOfColumns = 2;
int numberOfRows = 3;
public double cellWidth;
public double cellHeight;
// Constructor
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(SetGridCellWidthAndHeight);
}
void SetGridCellWidthAndHeight(object sender, RoutedEventArgs e)
{
cellWidth = GridWindows.ActualHeight / numberOfColumns;
cellHeight = GridWindows.ActualHeight / numberOfRows;
this.GridWindows.Children.Add(SetUpGridLayout());
}
private Grid SetUpGridLayout()
{
Grid grid = new Grid();
grid.Background = new SolidColorBrush(Colors.White);
// Create column and row definitions.
ColumnDefinition[] columnDefinition = new ColumnDefinition[numberOfColumns];
RowDefinition[] rowDefinition = new RowDefinition [numberOfRows];
for (int i = 0; i < columnDefinition.Count(); i++)
{
columnDefinition[i] = new ColumnDefinition();
grid.ColumnDefinitions.Add(columnDefinition[i]);
}
for (int i = 0; i < rowDefinition.Count(); i++)
{
rowDefinition[i] = new RowDefinition();
grid.RowDefinitions.Add(rowDefinition[i]);
}
int count = 1;
for (int row = 0; row < numberOfRows; row++)
{
for (int column = 0; column < numberOfColumns; column++)
{
StackPanel gridViewStackPlanel = new StackPanel();
gridViewStackPlanel.Background = new SolidColorBrush(Colors.White);
Button button = new Button();
button.Width = cellWidth*0.8;
button.Height = cellHeight *0.8;
//topicButton.Background = new SolidColorBrush(Colors.Red);
button.VerticalAlignment = VerticalAlignment.Center;
button.HorizontalAlignment = HorizontalAlignment.Center;
button.Background = new SolidColorBrush(Colors.Red);
//To display the Topic name
TextBlock name= new TextBlock();
name.Text = " Value" + count;
name.Foreground = new SolidColorBrush(Colors.Black);
name.HorizontalAlignment = HorizontalAlignment.Center;
gridViewStackPlanel.Children.Add(button);
gridViewStackPlanel.Children.Add(name);
count++;
grid.Children.Add(gridViewStackPlanel);
Grid.SetColumn(gridViewStackPlanel, column);
Grid.SetRow(gridViewStackPlanel, row);
}
}
return grid;
}
When you write a code like this:
button.Width = cellWidth * 0.8;
button.Height = cellHeight * 0.8;
You would get a square only if cellWidth == cellHeight. And that is most likely not true. So your widths and heights are different. Consider replacing the above with something like this:
cellWidth = Math.Min(GridWindows.ActualHeight / numberOfColumns, GridWindows.ActualHeight / numberOfRows);
button.Width = cellWidth * 0.8;
button.Height = cellWidth * 0.8;
Now it will be square.