How to get the value of a control added dynamically in Windows Form C #?

I read several articles and could not solve the problem. My problem is when I try to get the value of the controls (CheckBox and ComboBox) dynamically added to Windows Form, I need to know when the CheckBox is checked (or not checked), and if the ComboBox is empty (or not) when I click the button , this button calls a method in which I check if all components are empty, I add controls as follows:

CheckBox box; ComboBox cmBox; for (int i = 1; i <= sumOfRegisters; i++) { box = new CheckBox(); box.Name = "CheckBox" + i; box.Text = "Some text"; box.AutoSize = true; box.Location = new Point(10, i * 25); //vertical cmBox = new ComboBox(); cmBox.Name = "ComboBox" + i; cmBox.Size = new System.Drawing.Size(302, 21); cmBox.TabIndex = i; cmBox.Text = "Some Text"; cmBox.Location = new Point(270, i * 25); this.groupBox.Controls.Add(cmBox); this.groupBox.Controls.Add(box); } 

"I am adding values ​​from the database in the case of the ComboBox, I skipped this part."

I am trying to get the value using foreach:

 foreach (Control ctrl in groupBox.Controls) 

The problem is that I don’t know how to check if Control (CheckBox and ComboBox) is checked or empty (as in the case).

Really thanks for any help, I appreciate your time.

+7
c # checkbox winforms combobox
source share
5 answers

You can use the as operator, for example:

 foreach (Control ctrl in groupBox.Controls) { CheckBox checkBox = ctrl as CheckBox; ComboBox comboBox = ctrl as ComboBox; if (checkBox != null) // Control is a CheckBox { if (checkBox.Checked) { // CheckBox is checked } else { // CheckBox is not checked } } else if (comboBox != null) // Control is a ComboBox { if (comboBox.Items.Count == 0) { // ComboBox is empty } else { // ComboBox isn't empty } } } 
+2
source share

You drop your link to each dynamically allocated control after adding it to grbMateias.

One parameter, of course, is to store these links, for example, in a List<Control> .

 List<ComboBox> comboBoxes = new List<ComboBox>(); for (int i = 1; i <= sumOfRegisters; i++) { box = new CheckBox(); comboBoxes.Add(box); // etc. } 

However, you can just iterate over the controls in grbMateias

  foreach(Control ctl in grbMateias.Controls) { if (ctl is CheckBox) { // Use the checkbox } else if (ctl is ComboBox) { // Use the ComboBox } } 

or using Linq

 var comboBoxes = grbMateias.Controls.OfType<ComboBox>(); var checkBoxes = grbMateias.Controls.OfType<CheckBox>(); 
+3
source share

Since you give each control a name in a loop, it is easy to use it to recognize each individual control if the names are unique. For example, to get the third checkbox, you can use something like this:

 Control control = groupBox.Controls.Single(x => x.Name == "CheckBox3"); Checkbox thirdCheckBox = (CheckBox)control; bool checkBoxCheched = thirdCheckBox.Checked; 
+1
source share

First you need to know what control you are currently working with during the iteration, then you can use it and use certain control methods.

 foreach(Control c in groupBox.Controls) { if(c is ComboBox) { if((ComboBox) c).SelectedItem == null) //handle nothing entered } else if(c is CheckBox) { if((CheckBox) c).Checked) //handle checked } } 

In the above snippet, we check with the is keyword what type of control it has. Then, if it is a control that we know how to handle, we use it and check whether it is empty or checked.

+1
source share

I need to know when the CheckBox is installed (or not checked), and if the ComboBox is empty (or not).

To get the states of your checkboxes and comboboxes, including the index that you used to create them, you can do the following:

 var checkBoxStates = groupBox.Controls .OfType<CheckBox> .Where(x => x.Name.StartsWith("CheckBox")) .Select(x => new { Index = Int.Parse(x.Name.Substring("CheckBox".Length)), IsChecked = x.Checked }) .ToList(); var comboBoxStates = groupBox.Controls .OfType<ComboBox> .Where(x => x.Name.StartsWith("ComboBox")) .Select(x => new { Index = Int.Parse(x.Name.Substring("ComboBox".Length)), IsEmpty = x.Items.Count == 0 }) .ToList(); 
+1
source share

All Articles