473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need to expose specific properties for controls in a collection

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
1 1572
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: anon | last post by:
I am having a hard time with makeing what I am trying to do work. I am making a questionaire web app. I have a custom control that has a label and 5 radio buttons. My problem is that each...
1
by: Rahim | last post by:
i want to change all the label control style Properties, server control properties at runtime how should i call all the label at runtime, which is present at webform, any collections???? i...
2
by: Sky Sigal | last post by:
Hello: I'm currently messing around, and need as much feedback/help as I can get, trying to find the most economical/graceful way to build usercontrols that rely on styling to look any good... ...
7
by: Donald Grove | last post by:
Is it possible to retrieve field properties from a table in access2000 using code? I have tried: " dim dbs as dao.database dim tbl as dao.tabledef dim fld as dao.field dim prop as...
4
by: XNoDE | last post by:
Hi all, VS.Net 2002. Is there a way to set default properties for controls in the Toolbox? There a some properties that I implement as standard in my apps and would like to get around...
8
by: **Developer** | last post by:
I have a control called MyPanel that inherits from Panel. Also a control called MyRTB that inherits from RichTextBox. They are used on a form as follows: MyPanel in on the form and MyRTF is...
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
2
by: ljh | last post by:
I am using some 3rd party controls to create my own control. I want to "pass up" many (but not all) of the properties of the 3rd party controls that I am using to make my control, so that the end...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.