GroupBox and CheckBox c # winforms

0

I have a groupbox with several checkbox in, what I want to do is go through all the checkbox and get its status and see if it is selected or not and save in a variable that result (checked = 1, no checked = 0) to later use that variable and send to the BD the status of each of the checkbox .

Example: Groupbox1 inside I have:

Checkbox2
Checkbox3
Checkbox4
Checkbox5
Checkbox6
Checkbox7 

Thank you.

    
asked by Manny 05.08.2018 в 21:21
source

2 answers

0

This would be my solution, try to see if it works for you:

private class checkedBox
{
    public string name;
    public bool status;
}

List<checkedBox> listChekedBox;

public void findCheckedBox(GroupBox group)
{
    listChekedBox = new List<checkedBox>();
    foreach (Control control in group.Controls)
    {
        if (control.GetType() == typeof(CheckBox))
        {
            checkedBox box = new checkedBox();
            box.name = control.Name;
            box.status = ((CheckBox)control).Checked;
            listChekedBox.Add(box);
        }
    }
}

If you want it exactly as you indicate it, you should only change the variable type to whole:

private class checkedBox
{
    public string name;
    public int status;
}

List<checkedBox> listChekedBox;

public void findCheckedBox(GroupBox group)
{
    listChekedBox = new List<checkedBox>();
    foreach (Control control in group.Controls)
    {
        if (control.GetType() == typeof(CheckBox))
        {
            checkedBox box = new checkedBox();
            box.name = control.Name;
            box.status = ((CheckBox)control).Checked ? 1 : 0;
            listChekedBox.Add(box);
        }
    }
}
    
answered by 06.08.2018 / 15:53
source
0

Look, here is an example that you can implement in your project, in which the forms of a panel are covered:

int[] check =new int[5];
            foreach (Control ct in this.groupbox)
            {
                if (ct is CheckBox)
                {
                    if (((CheckBox)ct).Checked)
                    {
                        check[0] = 1;
                    }
                }
             }

you declare an array in which you store the values and then when you are going to insert in the DB you can go through the array and extract its respective value.

    
answered by 05.08.2018 в 23:40