How to obtain the value of a control added dynamically into Windows Form c#?
Asked Answered
M

5

7

I read some articles and don't managed solved my problem, My problem is in the moment when I try obtain the value of the controls (CheckBox and ComboBox) added dynamically into Windows Form, I need know when the CheckBox is checked (or unchecked) and if the ComboBox is empty (or not) when I press a button, this button call a method in which I validate if all components are empty, I add the controls the following way:

 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 add the values from database in the case of the ComboBox, I omitted this part."

I try obtain the value with a foreach:

foreach (Control ctrl in groupBox.Controls)

The problem is I don't have idea how to know if the Control (CheckBox and ComboBox) is checked or empty (as the case).

Really thanks for any help, I appreciate your time.

Mahound answered 4/5, 2015 at 23:40 Comment(3)
I recommend looking into the DataRepeater control.Thermogenesis
Is the issue you aren't sure what control is of what type when you're iterating through groupBox.Controls?Cassaundra
It might be worthwhile separating out the two controls in separate foreach statements: var CheckBox chk; foreach (chk in groupbox.Controls) and var ComboBox cmb; foreach (cmb in groupbox.Controls)Necessarian
P
2

You could use the as operator, like so:

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
        }
    }
}
Previous answered 4/5, 2015 at 23:47 Comment(1)
Thanks for your answer and your time, I solved my problem with your answer.Mahound
R
3

You are throwing away your reference to each dynamically allocated control after adding it to grbMateias.

One option is certainly to store those references, 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.
}

You can however just iterate 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>();
Rosario answered 4/5, 2015 at 23:46 Comment(0)
P
2

You could use the as operator, like so:

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
        }
    }
}
Previous answered 4/5, 2015 at 23:47 Comment(1)
Thanks for your answer and your time, I solved my problem with your answer.Mahound
A
1

Since you're giving each control a name within the loop, it's easy to use it to recognize each individual control, as long as names are unique. For example, to obtain 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;
Accountant answered 4/5, 2015 at 23:46 Comment(0)
C
1

First you need to know what control you're currently working with when iterating, then you can cast it and use that specific control's 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 snippit we check using the is keyword what type of Control it is. Then if it's of a control that we know how to handle we cast it and check if empty or checked.

Cassaundra answered 4/5, 2015 at 23:50 Comment(0)
G
1

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

To get the states of your checkboxes and comboboxes, including the index 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();
Gladiator answered 4/5, 2015 at 23:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.