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(o bject sender, System.EventArgs e)
{
foreach (Control X in CurrentPanel.Controls)
{
if ((X.GetType().ToString()) == ("System.Windows.Forms.Tex tBox"))
{
X.Enabled = !(X.Enabled);
}
}
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(o
{
foreach (Control X in CurrentPanel.Controls)
{
if ((X.GetType().ToString()) == ("System.Windows.Forms.Tex
{
X.Enabled = !(X.Enabled);
}
}
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);
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I love it when there's a simple solution...worked perfectly, thanks.
CType(X, TextBox).TextBoxOnlyProper
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.