Scan multiple checkboxes in a groupbox C #

0

Good morning !!

I have several checkboxes inside the same form in GroupBoxes separated by question. I would like to scan all of those checkboxes that are inside the GroupBoxes where if checked S will not receive N and the result will be in a concatenated String.

Example:

3 yes, 4 no: String result="SSSNNNN".

I have tried in many ways, one of them was:

private void button1_Click(object sender, EventArgs e)
{
    String meat = "";
    foreach (Control gb in this.Controls)
    {
        if (gb is GroupBox)
        {
            foreach (Control chk in gb.Controls)
            {
                if (chk is CheckBox)
                {
                    CheckBox c = chk as CheckBox;
                    if (c.CheckState == CheckState.Checked)
                    {
                        meat += "S";
                    }
                    else
                    {
                        meat += "N";
                    }
                    MessageBox.Show(meat);
                }
            }
        }
    }
}**

But the result comes out letter by letter and not all together.

    
asked by anonymous 01.08.2018 / 14:55

1 answer

0

You are viewing the message within the loop. Just show it off:

String meat = "";
foreach (Control gb in this.Controls)
{
    if (gb is GroupBox)
    {
        foreach (Control chk in gb.Controls)
        {
            if (chk is CheckBox)
            {
                CheckBox c = chk as CheckBox;
                meat += c.Checked ? "S" : "N";
            }
        }
    }
}
MessageBox.Show(meat);
    
01.08.2018 / 15:00