?
Solved

changing properties for all controls in a collection

Posted on 2003-03-11
4
Medium Priority
?
157 Views
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);
        }
      }

0
Comment
Question by:jtdagger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Hummusx
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.
0
 

Author Comment

by:jtdagger
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);
        }
      }
0
 
LVL 10

Accepted Solution

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

((TextBox)x).ReadOnly=true;

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.

Smg.
0
 

Author Comment

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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 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