Michael
Michael

Reputation: 13636

GroupBox contain controls,how to get the names of those controls?

Im using GroupBox Control on my Form Page(WinForms).

The GroupBox contain five controls(RadioButtons).

Any idea how can I get the names and the states of controls inside GroupBox Control?

Upvotes: 5

Views: 18687

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460288

You can use Enumerable.OfType to find and cast your RadioButtons in the GroupBox:

var radioButtons = groupBox1.Controls.OfType<RadioButton>();
foreach (RadioButton rb in radioButtons)
{
    bool state = rb.Checked;
    string name = rb.Name;
}

Upvotes: 15

indiPy
indiPy

Reputation: 8072

       var grpBox = new GroupBox();
        if (grpBox.HasChildren)
        {
            var name = grpBox.Controls[0].Name;
        }

Upvotes: 1

Related Questions