Determine control type while iterating through a collection

G

Guest

I trigger this action from a radio button included in several panels. I want
to disable all controls inside the panel when the radio button indicates the
group should be inactive. This solution works but it reeks of a hack borne of
my lack of experience working with >NET components.

Any suggestions for a more elegant (efficient) solution are appreciated.

<code>
private void rbFilter_Panel_CheckedChanged(object sender, System.EventArgs
e)
{ // used for any number of Panels containing filter expression builder
boxes
RadioButton rb = (RadioButton)sender; // we know the sender will always
be a RadioButton
Panel p = (Panel) rb.Parent; // the sender is a RadioButton contained in
a Panel
foreach( Control c in p.Controls ) // Panel has a collection of Controls
{ // we don't want to disable any RadioButtons - expect only ComboBox and
TextBox
string s = c.GetType().ToString();
if (s == "System.Windows.Forms.ComboBox" ||
s == "System.Windows.Forms.TextBox" )
{ // only change the TextBox and ComboBox components in the panel
c.Enabled = ! rb.Checked;
}
}
}
</code>
 
G

Guest

Thanks. That's just the operation I hoped for. I didn't want to nest another
panel inside the panel containing the controlling radio button - it just
seemed to be cascading complexity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top