Changing the properties of some controls on a form

D

Dan Soendergaard

I have this code:

for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls is ValidationTextBox)
{
// Change the property of the control here.
}
}

ValidationTextBox is a control which derives from TextBox. It contain
some custom properties, among them: ValidValueColor.

As you see, I'm trying to change a property of all instances of
ValidationTextBox in my form. When I tried to do
this.Controls.ValidValueColor, I found out that the property wasn't
available. How can I access the custom properties from the
ValidationTextBox?

Thanks in advance!

Dan
 
V

VJ

this.Controls <--- returns type Control.. so you try something like
((ValidationTextBox)this.Controls).ValidValueColor, that will work. To
avoid expection include type of check to ensuer that it is correct tpye.. if
(this.Control.GetType() == typeof(ValidationTextBox) )

Vijay
 
B

Bruce Wood

I have this code:

for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls is ValidationTextBox)
{
// Change the property of the control here.
}
}

ValidationTextBox is a control which derives from TextBox. It contain
some custom properties, among them: ValidValueColor.

As you see, I'm trying to change a property of all instances of
ValidationTextBox in my form. When I tried to do
this.Controls.ValidValueColor, I found out that the property wasn't
available. How can I access the custom properties from the
ValidationTextBox?


Try this:

foreach (Control c in this.Controls)
{
ValidationTextBox vbox = c as ValidationTextBox
if (vbox != null)
{
// Change the property of the "vbox" control
here.
}
}
 
D

Dan Soendergaard

I have this code:
for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls is ValidationTextBox)
{
// Change the property of the control here.
}
}

ValidationTextBox is a control which derives from TextBox. It contain
some custom properties, among them: ValidValueColor.
As you see, I'm trying to change a property of all instances of
ValidationTextBox in my form. When I tried to do
this.Controls.ValidValueColor, I found out that the property wasn't
available. How can I access the custom properties from the
ValidationTextBox?


Try this:

foreach (Control c in this.Controls)
{
ValidationTextBox vbox = c as ValidationTextBox
if (vbox != null)
{
// Change the property of the "vbox" control
here.
}
}


Thanks, worked just fine!
 

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