I am creating a five checkboxes dynamiclly with a for loop inside a GroupBox.
now, they were created dynamiclly, so I don't know how can I make a ChangeCheck method attached to them?
All those checkoxes are related so what I am trying to do is something like that:
create 5 checkboxes dynamiclly
add each checkbox to a list
when a specific checkbox in the list is on trigger a method.
this is how I create the checkboxes:
for (int i = 0; i < 5; i++)
{
CheckBox chk = new CheckBox();
chk.size = new Size(10, 10);
chk.Top = 10
chk.Left = 20
chk.Text = i.ToString();
group_box_name.controls.Add(chk);
}
Now, how can I detect which checkbox was on/off?
You could do the following:
int top = 0;
for (int i = 0; i < 5; i++)
{
CheckBox chk = new CheckBox();
chk.size = new Size(10, 10);
chk.Top += (5 + 10); //Spacing = 5, CheckboxHeight = 10
chk.Left = 20;
chk.Text = i.ToString();
chk.CheckedChanged += CheckBox_CheckedChanged;
chk.Tag = i;/*You can put anything here.
Otherwise you could also use the Name property..
It just helps to determine which checkbox was currently checked */
group_box_name.controls.Add(chk);
}
private void checkBox_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
if(cbx != null)
{
int tag = int.Parse(cbx.Tag.ToString());
switch(tag)
{
case 0:
//Do whatever:
break;
//Handle other cases here:
}
}
}
Add checked change event to your dynamically created checkbox. You can add name also
for (int i = 0; i < 5; i++)
{
CheckBox chk = new CheckBox();
chk.Name = "chk" + i.ToString();
chk.size = new Size(10, 10);
chk.Top = 10
chk.Left = 20
chk.Text = i.ToString();
chk.CheckedChanged += checkBox_CheckedChanged;
group_box_name.controls.Add(chk);
}
private void checkBox_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox chk=sender as CheckBox;
if(chk!=null)
{
if(chk.Checked)
{
string chkName=chk.Name;
string chkText=chk.Text;
//your code
}
}
}
Related
I wanna change the Text properties of Label using Buttons just like in hangman; but after I created the Label, I became confused when I try to access the specific Label
// creating label
for (int i = 0; i < numericUpDown1.Value; i++)
{
Label l = new Label();
l.Text = "_";
l.Width = 20;
l.Height = 25;
l.Left = i * 20 + 510;
l.Top = 20;
l.BackColor = Color.Transparent;
groupBox2.Controls.Add(l);
}
// function to change the label text
// if I clicked the button
// the first label text will be changed to the text in the button i clicked
private void B_Click(object sender, EventArgs e)
{
var thsBtn = (Button)sender;
bool benar = false;
if (benar == false)
{
thsBtn.Text = " ";
thsBtn.Enabled = false;
}
else
{
thsBtn.Enabled = false;
}
}
You can organize created Labels into a collection, say, List<Label>:
private List<Label> m_CreatedLabels = new List<Label>();
...
// Remove all previous labels
foreach (Label lbl in m_CreatedLabels)
lbl.Dispose();
m_CreatedLabels.Clear();
// Create new ones
for (int i = 0; i < numericUpDown1.Value; i++) {
m_CreatedLabels.Add(new Label() {
Text = "_",
Width = 20,
Height = 25,
Left = i * 20 + 510,
Top = 20,
BackColor = Color.Transparent,
Parent = groupBox2
});
}
Now you have m_CreatedLabels collection to work with created Labels, e.g.
private void B_Click(object sender, EventArgs e) {
var thsBtn = sender as Button;
// you may want to add a condition into FirstOrDefault(), e.g.
// .FirstOrDefault(lbl => lbl.Text == "_")
// - first label with "_" Text
Label lblToProcess = m_CreatedLabels
.FirstOrDefault();
if (null != lblToProcess)
lblToProcess.Text = thsBtn.Text;
thsBtn.Enabled = false;
}
One option here is to give your dynamically created Label instances a Name. From there, you should be able to use ControlCollection.Find to find your Label instances by name.
private void CreateLabels()
{
for (int i = 0; i < numericUpDown1.Value; i++)
{
Label l = new Label();
l.Name = $"DynamicLabel{i}";
l.Text = "_";
l.Width = 20;
l.Height = 25;
l.Left = i * 20 + 510;
l.Top = 20;
l.BackColor = Color.Transparent;
groupBox2.Controls.Add(l);
}
}
private void DoSomethingWithADynamicLabel(int dynamicLabelIndex)
{
Label l = groupBox2.Controls.Find($"DynamicLabel{i}", true).FirstOrDefault() as Label;
if (l is null)
{
// Couldn't find the label...
return;
}
// Do something with l
}
When creating the Label instances inside CreateLabels, I'm simply appending the for loop's counter to the string "DynamicLabel". This gives you a bunch of Labels with names like "DynamicLabel0", "DynamicLable1", "DynamicLabel2", etc...
Then in DoSomethingWithADynamicLabel, assuming you have the index of the Label you want to deal with, you can use groupBox2.Controls.Find to actually find the Label you're interested in. ControlCollection.Find returns Control[], so calling FirstOrDefault will take the first item from the array or null if no Control with the given name exists.
Hypocritically speaking I have a job that is creating ideas for my clients.
For one client I have a list of suggestions, from which I created checkboxs accordingly.
Now I need to know which of those are unchecked by the client, so I can delete them.
-The number of suggestions is not fixed. The number is the length of my current SuggestionList.
int checkboxnumber = Myclass.suggestionline.Count();
for (int i = 0; i < checkboxnumber; i++)
{
CheckBox cb = new CheckBox();
cb.Text = Myclass.suggestionline[i][0];
cb.Location = new Point(5, 5 + i * 24);
cb.BackColor = Color.White;
cb.Name = "checkbox"+i;
cb.AutoSize = true;
cb.Checked = true;
panel1.Controls.Add(cb);
};
I structured my SuggestionList as List < List < string > >, a 4 suggestions SuggestionList example would be :
{{"suggestion1", "like", "100 Euro"},{"suggestion2", "like", "200 Euro"},{"suggestion3", "like", "300 Euro"},{"suggestion4", "like", "400 Euro"}}
I figured it out. Thanks to this related question: How can I create a dynamic button click event on a dynamic button?
CheckBox cb = new CheckBox();
...
int checkboxnumber = Myclass.suggestionline.Count();
for (int i = 0; i < checkboxnumber; i++)
{
cb = new CheckBox();
cb.Text = Myclass.suggestionline[i][0];
cb.Location = new Point(5, 5 + i * 24);
cb.BackColor = Color.White;
cb.Name = "checkbox"+i;
cb.AutoSize = true;
cb.Checked = true;
cb.CheckedChanged += new EventHandler(CheckedChanged);
panel1.Controls.Add(cb);
};
...
private void CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
if (!cb.Checked)
{
Console.WriteLine(cb.Text);
}
}
Hi my problem is:I create GroupBoxs dynamically and I add into the groupboxs a number of controls in (my case radioButtons ) I'd like the groupboxs to size dynaically in order to display all the RadioButtons that I insert into them. How can I do?
This is the code:
private void IdEnForm_Load(object sender, EventArgs e)
{
for (int i = 0; i < a.Count; i++)
{
for (int j = 0; j < a[i].Count; j++)
{
bool help;
if (j == 0) help = true;
else help = false;
if (help)
{
gb = new GroupBox();
gb.Text = " which Entity you want to mantain?";
gb.Font = new Font("Calibri", 12);
gb.AutoSize = true;
gb.Location = new Point(j * 150, (i + 1) * 100);
}
RadioButton rb = new RadioButton();
rb.Text=""+ a[i][j];
rb.AutoSize = true;
gb.Controls.Add(rb);
this.Controls.Add(gb);
// MessageBox.Show("" + a[i][j]);
}
enter code here
You want to create an Event Handler to trigger When a Control is added to the groupbox like this:
groupbox.ControlAdded += new ControlEventHandler( groupbox_ControlAdded );
And then add a method to deal with the event:
void groupbox_ControlAdded( object sender, ControlEventArgs e )
{
//Do Resizing here
}
I descripted my problem in script comments. Function GraphicClassStructure is only for create buttons and label. But mainly function is plusButton_click. I need that if i clicked on first plus button so i need change text for first added label.
Script: (Problem is here: plusButton_click, i descripted problem to case)
class GraphicClassStructure : GraphicPosition
{
// Button
public Button plus;
public PictureBox classBackround = new PictureBox();
// Label
Label points;
public void CreateSpellsButton()
{
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 10; i++)
{
plus = new Button();
points = new Label();
switch (j)
{
case 0:
points.Location = Location[1][i];
points.Location = new Point(points.Location.X + 8, points.Location.Y + 45);
plus.Location = Location[2][i];
break;
case 1:
plus.Location = Location[2][i];
plus.Location = new Point(plus.Location.X + 205, plus.Location.Y);
points.Location = Location[1][i];
points.Location = new Point(points.Location.X + 213, points.Location.Y + 45);
break;
case 2:
plus.Location = Location[2][i];
plus.Location = new Point(plus.Location.X + 410, plus.Location.Y);
points.Location = Location[1][i];
points.Location = new Point(points.Location.X + 418, points.Location.Y + 45);
break;
}
// Labels for point
points.BackColor = Color.Transparent;
points.ForeColor = Color.FromArgb(((int)(((byte)(193)))), ((int)(((byte)(196)))), ((int)(((byte)(181)))));
points.BackgroundImageLayout = ImageLayout.Stretch;
points.FlatStyle = FlatStyle.Flat;
points.Name = "points";
points.Name = spells.Name + i.ToString() + "_" + j.ToString();
if (i >= 6)
points.Text = "0 / 2";
else
points.Text = "0 / 1";
points.VisibleChanged += new EventHandler(classUniqueButtons_VisibleChanged);
// Plus
plus.BackColor = Color.Transparent;
plus.BackgroundImage = BuildResource.plus;
plus.BackgroundImageLayout = ImageLayout.Stretch;
plus.FlatAppearance.BorderSize = 0;
plus.FlatAppearance.MouseDownBackColor = Color.Transparent;
plus.FlatAppearance.MouseOverBackColor = Color.Transparent;
plus.FlatStyle = FlatStyle.Flat;
plus.Name = "plus";
plus.Size = Size[0][9];
plus.UseVisualStyleBackColor = false;
plus.Name = plus.Name + i.ToString() + "_" + j.ToString();
plus.Click += new EventHandler(plusButton_click);
plus.VisibleChanged += new EventHandler(classUniqueButtons_VisibleChanged);
plus.MouseEnter += new EventHandler(this.classButton_MouseEnter);
plus.MouseLeave += new EventHandler(this.classButton_MouseLeave);
classBackround.Controls.Add(plus);
classBackround.Controls.Add(points);
}
}
}
private void plusButton_click(object sender, EventArgs e)
{
var currentButton = sender as Button;
var name = currentButton.Name;
switch (name)
{
// Ultimate
case "plus0_0":
// If i clicked on this button so i need change text for first added label
// I try this, but it changed only last added labe
points.Text = "test";
break;
case "plus0_1":
// If i clicked on this button so i need change text for second added label
break;
}
}
private void minusButton_click(object sender, EventArgs e)
{
var currentButton = sender as Button;
var name = currentButton.Name;
switch (name)
{
// Ultimate
case "minus0_0":
// If i clicked on this button so i need change text for first added label
// I try this, but it changed only last added labe
points.Text = "test";
break;
case "minus0_1":
// If i clicked on this button so i need change text for second added label
break;
}
}
}
very simple to do here what you need
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 10; i++)
{
plus = new Button();
//tag is an object and can be used to reference any other object
plus.Tag = new Label();
and to retrieve what you need do this
private void plusButton_click(object sender, EventArgs e)
{
var currentButton = sender as Button;
var name = currentButton.Name;
switch (name)
{
// Ultimate
case "plus0_0":
(currentButton.Tag as Label).Text = "test";
break;
case "plus0_1":
(currentButton.Tag as Label).Text ="test2"
break;
}
}
You are using a global reference to the label which you have overwritten in each iteration of your loop. Therefore it always references the last label. Since you always add the label after the button and have the reference to the button you can probably get the correct label like this:
Label pointsLabel = (Label)classBackround.Controls[classBackround.Controls.IndexOf(currentButton) + 1]
I am trying to add controls to a panel from a foreach loop.
When i press the button i want every element from a array to show as checkbox. This is wordking fine, then i want a numeric updown behind the checkbox so users can select a value.
The code for creating the checkboxes works just fine, for every item in my array it displays e checkbox. But it only shows 1 NumericUpDown.
Can anybody tell me why it only shows 1 numeric updown, while it shows all of the checkboxes?
Here is my code:
private void bierButton_Click(object sender, EventArgs e)
{
int height = 1;
int padding = 10;
int i = 0;
int x = 0;
CheckBox[] chk = new CheckBox[10];
NumericUpDown[] nmr = new NumericUpDown[10];
orderBox.Clear();
hideBtn();
foreach (string bieren in Drinks.bier)
{
chk[i] = new CheckBox();
nmr[i] = new NumericUpDown();
chk[i].Name = i.ToString();
chk[i].Text = Drinks.bier[i];
chk[i].TabIndex = i;
chk[i].AutoCheck = true;
chk[i].Bounds = new Rectangle(10, 0 + padding + height, 200, 22);
panel1.Controls.Add(chk[i]);
testPanel.Controls.Add(nmr[i]);
height += 22;
i++;
}
}
It appears you are not updating the position of your NumericUpDown controls.
All of them are there, they are just on top of one another.
Consider this change:
private void bierButton_Click(object sender, EventArgs e)
{
int height = 1;
int padding = 10;
int i = 0;
int x = 0;
CheckBox[] chk = new CheckBox[10];
NumericUpDown[] nmr = new NumericUpDown[10];
orderBox.Clear();
hideBtn();
foreach (string bieren in Drinks.bier)
{
chk[i] = new CheckBox();
nmr[i] = new NumericUpDown();
chk[i].Name = i.ToString();
chk[i].Text = bieren; // Drinks.bier[i];
chk[i].TabIndex = i;
chk[i].AutoCheck = true;
chk[i].Bounds = new Rectangle(10, 0 + padding + height, 200, 22);
// Start New Code
nmr[i].Bounds = new Rectangle(10, 0 + padding + height, 200, 22);
// End New Code
panel1.Controls.Add(chk[i]);
testPanel.Controls.Add(nmr[i]);
height += 22;
i++;
}
}
I also changed one line to chk[i].Text = bieren;.