Link to home
Start Free TrialLog in
Avatar of jtdagger
jtdagger

asked on

changing properties for all controls in a collection

I have approximately 20 textboxes on a Windows form that I need to be able to manipulate properties for.

The following code works fine for setting the 'Enabled' property.  How can I use a collection of textboxes to change the 'ReadOnly' and 'BorderStyle' properties?

    private void ButtonLockControls_Click(object sender, System.EventArgs e)
    {
      foreach (Control X in CurrentPanel.Controls)
      {
        if ((X.GetType().ToString()) == ("System.Windows.Forms.TextBox"))
        {
          X.Enabled = !(X.Enabled);
        }
      }

Avatar of Hummusx
Hummusx

To get at the more class-specific properties, use Ctype.

CType(X, TextBox).TextBoxOnlyProperty = whatever;

Also, I would recommend you compare the actual types instead of converting to strings.  In this case it probably doesn't make much difference, but it's much safer not to have a bunch of strings running around in your code that could potentially need changing if the class hierarchy ever changed.
Avatar of jtdagger

ASKER

I appreciate the advice on comparing types, but I'm not sure how CType would solve my problem.  Isn't CType only supported in VB?

Basically, I just want a button event that will toggle the ReadOnly property of all the textboxes in a panel.


      foreach (Control ctl in CurrentPanel.Controls)
      {
        if (ctl is TextBox)
        {
          ctl.Enabled = !(ctl.Enabled);
        }
      }
ASKER CERTIFIED SOLUTION
Avatar of smegghead
smegghead
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I love it when there's a simple solution...worked perfectly, thanks.