I'm sorry if this seems n00bish, but I have been searching for this for a few days now. I am attempting to dynamically add checkboxes to a windows form; however, only one checkbox appears on the form. Here is my code:
for (int i = 0; i < 10; i++)
{
box = new CheckBox();
box.Tag = i.ToString();
box.Text = "a";
box.AutoSize = true;
box.Location = new Point(10, i + 10);
Main.Controls.Add(box);
}
As you can see I am adding the checkboxes via a for loop. I have tried messing with the location and enabling autosize in case they were somehow overlapping. The result is a single checkbox with text "a".
Actually you already created a CheckBox but within the same point.
CheckBox box;
for (int i = 0; i < 10; i++)
{
box = new CheckBox();
box.Tag = i.ToString();
box.Text = "a";
box.AutoSize = true;
box.Location = new Point(10, i * 50); //vertical
//box.Location = new Point(i * 50, 10); //horizontal
this.Controls.Add(box);
}
In this case with help of dynamically assign Name property how to achive checkbox.checked property , in some other action like submit button. how can i get all check box is checked and which is created in loop?
If you have an instance from every button you can make with your button or your event to make something like
CheckBox myCheckedBox = (CheckBox)sender;
Related
Hey I would like to create a stack panel with multiple buttons in it and when I press one, it tells me which button it is. Like when I press the second button from the top, I get the number 2 back and so on.
The buttons are created with this loop:
for (int i = 0; i < LBresponse.Items.Count; i++)
{
System.Windows.Controls.Button BTclear = new Button();
BTclear.Content = "Clear";
BTclear.Width = 50;
BTclear.Height = 20;
BTclear.HorizontalAlignment = HorizontalAlignment.Right;
BTclear.Click += Button_Click;
BTclear.IsEnabled = true;
STPresponse.Children.Add(BTclear);
}
I could do it with the location of the button but I hope there is a better solution.
One Idea would be to put the number into the context when the button is created but this would not be pretty.
You could store the number or index somewhere, for example in the Tag property:
for (int i = 0; i<LBresponse.Items.Count; i++)
{
System.Windows.Controls.Button BTclear = new Button();
BTclear.Tag = i;
BTclear.Content = "Clear";
BTclear.Width = 50;
BTclear.Height = 20;
BTclear.HorizontalAlignment = HorizontalAlignment.Right;
BTclear.Click += (ss, ee) =>
{
MessageBox.Show(((Button)ss).Tag.ToString());
};
BTclear.IsEnabled = true;
STPresponse.Children.Add(BTclear);
}
I have a split container panel in GUI Form.When a button is clicked in left panel,it pulls out information along with multiple checkboxes on dynamically created panels on the right panel using a loop.Each panel on the right side can have multiple checkboxes based on some condition.For example, the first panel has one checkbox and the second panel below the first has got 8 checkboxes in the same row.When one of the checkboxes in second panel is clicked, I have to get the index of the that panel to do some manipulation. Tab index does not help me to get one as each panel can have any number of checkboxes.I spent a day to get around the problem with no luck. Your help will be much appreciated. I have posted the code below.
for (int j = 0; j < numOfSensors.Count; j++)
{
sensorpanel = new Panel();
sensorpanel.Size = new Size(800, 60);
sensorpanel.Location = new Point(0, Y);
sensorpanel.BackColor = Color.LightGray;
sensorpanel.Paint += new PaintEventHandler(panel_Paint);
Button sensor = new Button();
sensor.Size = new Size(200, 50);
sensor.Location = new Point(1, 1);
sensor.FlatStyle = FlatStyle.Flat;
sensor.FlatAppearance.BorderColor = Color.LightGray;
String sensorType = "Occupancy";
sensor.TextAlign = ContentAlignment.MiddleLeft;
sensor.BackColor = Color.LightGray;
sensor.ForeColor = Color.Black;
sensorpanel.Controls.Add(sensor);
if (sensorType.Equals("Occupancy"))
{
CheckBox cb = new CheckBox();
cb.Location = new Point(380, 15);
cb.Size = new Size(20, 17);
cb.Checked = checkBox(j,0);
cb.Text = "Occupancy";
checksensorbuttons.Add(cb);
cb.CheckedChanged += new EventHandler(cb_CheckChanged);
sensorpanel.Controls.Add(cb);
}
else if (sensorType.Equals("Multi-input:Digital"))
{
int xLoc = 210;
int yLoc = 15;
for (int k = 16; k <32; k+=2)
{
CheckBox cb = new CheckBox();
cb.Location = new Point(xLoc, yLoc);
cb.Size = new Size(20, 17);
cb.Checked = checkBox(j,k);
cb.Text = "Multi-input";
cb.CheckedChanged += new EventHandler(cb_CheckChanged);
sensorpanel.Controls.Add(cb);
xLoc += 30;
}
splitContainer.panel2.Controls.Add(sensorpanel);
}
//checkedchanged eventhandler
private void cb_CheckChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
if (checkbox.Text.Equals("Occupancy"))
{
// How to get the index of the panel when a checkbox in corresponding panel is checked?
if (checkbox.Checked == true)
{
//some manipulation
}
else
{
//some manipulation
}
}
else if (checkbox.Text.Equals("Multi-input"))
{
//get index of the panel where one of the checkboxes are clicked
if (checkbox.Checked == true)
{
//do some manipulation
}
else
{
//do some manipulation
}
}
There are multiple ways
Use Name property of the checkbox. When the checkbox is created assign it a logical name which makes it easier to identify it. Only string can be used here.
Use Tag property of the checkbox to preserve the data you want to refer it. Its good place to store any object and not just string.
Use Parent property of the checkbox to access checkbox's parent properties.
As a best practice, assign a logical name to the dynamic controls. This is the best way to identify the control.
sensorpanel.Name = "panel" + j.ToString();
this.Controls.Add(sensorpanel);
:
:
cb.Text = "Occupancy";
cb.Tag = "panel Index = " + j.ToString();
cb.Name = "panel" + j.ToString() + "_" + "cb_" + cb.Text;
sensorpanel.Controls.Add(cb);
void cb_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
string mssg;
mssg = "Name = " + checkbox.Name;
mssg = "tag = " + checkbox.Tag;
mssg = "Parent text = " + checkbox.Parent.Text;
mssg = "Parent name = " + checkbox.Parent.Name;
MessageBox.Show(mssg);
}
I'm supposed to create a magic square in 2D using Windows Forms Application. It should look like this:
However, the user should be able to decide the size of the square (3x3, 5x5, 7x7, etc). I already wrote the code in a Console Application, but I don't know how to add the 2D graphics.
Somebody already asked this question (How do I put my result into a GUI?), and one of the answers was to use DataGridView, but I'm not sure if that's what I'm looking for, since I can't make it look like the picture.
Any ideas or advice?
You can use a TableLayoutPanel and add buttons to panel dynamically.
If you don't need interaction with buttons, you can add Label instead.
Create square dynamically:
public void CreateSquare(int size)
{
//Remove previously created controls and free resources
foreach (Control item in this.Controls)
{
this.Controls.Remove(item);
item.Dispose();
}
//Create TableLayoutPanel
var panel = new TableLayoutPanel();
panel.RowCount = size;
panel.ColumnCount = size;
panel.BackColor = Color.Black;
//Set the equal size for columns and rows
for (int i = 0; i < size; i++)
{
var percent = 100f / (float)size;
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, percent));
panel.RowStyles.Add(new RowStyle(SizeType.Percent, percent));
}
//Add buttons, if you have your desired output in an array
//you can set the text of buttons from your array
for (var i = 0; i < size; i++)
{
for (var j = 0; j < size; j++)
{
var button = new Button();
button.BackColor = Color.Lime;
button.Font = new Font(button.Font.FontFamily, 20, FontStyle.Bold);
button.FlatStyle = FlatStyle.Flat;
//you can set the text of buttons from your array
//For example button.Text = array[i,j].ToString();
button.Text = string.Format("{0}", (i) * size + j + 1);
button.Name = string.Format("Button{0}", button.Text);
button.Dock = DockStyle.Fill;
//If you need interaction with buttons
button.Click += b_Click;
panel.Controls.Add(button, j, i);
}
}
panel.Dock = DockStyle.Fill;
this.Controls.Add(panel);
}
If you need interaction with buttons
void button_Click(object sender, EventArgs e)
{
var button = (Button)sender;
//Instead put your logic here
MessageBox.Show(string.Format("You clicked {0}", button.Text));
}
As an example, you can call
CreateSquare(3);
Screenshot:
You can create a Form and add a TableLayoutPanel with this property
tableLayoutPanel1.Dock = DockStyle.Fill;
tableLayoutPanel1.BackColor = Color.Gold;
and this is the result
When you create Row and Column, to fit correctly set the percentage in this way:
After this you can add a Button or Label in each square.
I am trying to create a series of RadioButtons on a WinForm. It works OK, but in the Click-event I want to capture the product ID and do stuff with that.
I am used to HTML-elements, and assigning data to a label and value for the RadioButton. With WinForms I cannot see the equivalent to the value attribute.
Any good advice on how to pass onn the product ID to the RadioButton Change-event?
var products = new Business.Products().GetAll();
if (!products.Any())
GrpCategories.Controls.Clear();
int y = 2;
int x = 2;
foreach (var product in products)
{
var btn = new RadioButton();
btn.Width = 100;
btn.Height = 20;
if (y >= GrpCategories.Height - btn.Height - 10)
{
x += btn.Width + 2;
y = 2;
}
y += btn.Height + 2;
btn.Appearance = Appearance.Button;
btn.Text = product.Name;
btn.Name = "BtnProduct_" + product.ID;
btn.Location = new Point(x, y);
GrpCategories.Controls.Add(btn);
}
Simply use the Tag-property of the RadioButton. This property can store any .NET object.
There is nothing like RadioButtonList control in winform or else it would have solved your problem like nothing.
But,then also You can group radio buttons by adding them inside a container such as a Panel control, a GroupBox control, or a form.
Drag a group panel inside your form and then add your radio buttons, this will group all the radio buttons inside that group.. as is done here.
I've a TableLayoutPanel that has 10 rows and 2 columns.
My problem is, when I add a label in the first column and a button in the other, everything is fine. But when I add another label and a button on e.g. the next row, the previous rows are empty. It seems that my program only shows to last added row.
Code:
Label lblProjectName = new Label();
lblProjectName.Text = "test";
lblProjectName.Anchor = AnchorStyles.Left;
Button btnProject = new Button();
btnProject.Text = "Fill In";
tlpProject.Controls.Add(lblProjectName, 0, 0);
tlpProject.Controls.Add(btnProject, 1, 0);
tlpProject.Controls.Add(lblProjectName, 0, 1);
tlpProject.Controls.Add(btnProject, 1, 1);
Tnx!
You have to create a new control for each cell in the panel.
Currently, your code is moving the controls to the last column and last row.
Something like this:
for (int i = 0; i < numRows; ++i) {
Button btnProject = new Button();
btnProject.Text = "Fill In";
tlpProject.Controls.Add(btnProject, 1, i);
}
Any event handlers would have to be added here too, where on the handler end, you have to check the "sender" of the object to see which button the user pressed.
It seems like Label and Button can only be children to single control while you are adding them into different cells(thus different controls). If you need all cells filled with Labels and Buttons you will need 10 Labels and 10 Buttons for each column. Look at the code:
for(int i=0;i<10;i++){
var lblProjectName = new Lable();
lblProjectName.Text = "test";
lblProjectName.Anchor = AnchorStyles.Left;
var btnProject = new Button();
btnProject.Text = "Fill In";
tlpProject.Controls.Add(lblProjectName, 0, i);
tlpProject.Controls.Add(btnProject, 1, i);
}