By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Need to expose specific properties for controls in a collection

P: n/a
I'm relatively new to C#, (I have to use it, now that I'veinherited someone else's projects). I'm looking for a way toiterate through the controls on a form; if the control is of acertain type, I want to set a property that is specific to thatcontrol type. The problem I'm having is that the "generic"Control object might not
contain the properties that I want to set.

Here's an example of what I'd like to try to do:

//loop through all controls on a form
foreach(Control ctrl in this.Controls)
{
//if control is a textbox
if(ctrl is TextBox)
{
//set a property that is specific to that control type
//If ctrl is a Textbox, set the ReadOnly property to True
ctrl.ReadOnly = true;
}
}

That is the general idea that I have; unfortunately, I'm notcertain that it's
possible to do this. Any help would be appreciated.

Thanks!

--------------------------------
From: Stefan W

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>MKr8pBYVfUC8jgultRFtGw==</Id>
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Stefan,

In this case, all you have to do is cast the instance of control to the
type specific instance, like so:

//loop through all controls on a form
foreach(Control ctrl in this.Controls)
{
//if control is a textbox
if(ctrl is TextBox)
{
//set a property that is specific to that control type
//If ctrl is a Textbox, set the ReadOnly property to True
((TextBox) ctrl).ReadOnly = true;
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Stefan W via .NET 247" <an*******@dotnet247.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
I'm relatively new to C#, (I have to use it, now that I've inherited someone
else's projects). I'm looking for a way to iterate through the controls on
a form; if the control is of a certain type, I want to set a property that
is specific to that control type. The problem I'm having is that the
"generic" Control object might not
contain the properties that I want to set.

Here's an example of what I'd like to try to do:

//loop through all controls on a form
foreach(Control ctrl in this.Controls)
{
//if control is a textbox
if(ctrl is TextBox)
{
//set a property that is specific to that control type
//If ctrl is a Textbox, set the ReadOnly property to True
ctrl.ReadOnly = true;
}
}

That is the general idea that I have; unfortunately, I'm not certain that
it's
possible to do this. Any help would be appreciated.

Thanks!

--------------------------------
From: Stefan W

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>MKr8pBYVfUC8jgultRFtGw==</Id>
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.