Scan multiple checkboxes in a C#groupbox

Asked

Viewed 100 times

0

Good morning!!

I have several checkboxes within the same form in Groupboxes separated by second. I would like to scan all these checkboxes that are inside the groupboxes where if checked receive S if not receiving N and the result, stay in a concatenated string.

Example:

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

I’ve tried several ways, one of her 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.

1 answer

0

You are displaying the message inside the loop. Just display it outside:

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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.