Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


changing properties for all controls in a collection

Posted on 2003-03-11
Medium Priority
Last Modified: 2010-04-15
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);

Question by:jtdagger
  • 2

Expert Comment

ID: 8114101
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.

Author Comment

ID: 8114974
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);
LVL 10

Accepted Solution

smegghead earned 200 total points
ID: 8115734
use casting rather than CType i.e.


this will convert x into a textbox .. but will throw an exception if x isn't a textbox, so you'd need to ensure that it's a textbox b4 calling this.


Author Comment

ID: 8116574
I love it when there's a simple solution...worked perfectly, thanks.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question