Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2004

Tip: Looking for answers? Try searching our database.

How to identify controls in a collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amil - 13 Oct 2004 17:55 GMT
Hi all,

I have a user control that has a button and five labels. Using foreach to
loop through all the controls, how do I identify all the labels apart from
the button?
Imran Koradia - 13 Oct 2004 18:03 GMT
For Each Ctrl As Control In UserControl.Controls
   If TypeOf Ctrl Is Button Then
       ' its a button
   ElseIf TypeOf Ctrl Is Label Then
       ' its a label
   ElseIf TypeOf Ctrl Is TextBox Then
       ' its a textbox
   ...
   End If
Next Ctrl

hope that helps..
Imran.

> Hi all,
>
> I have a user control that has a button and five labels. Using foreach to
> loop through all the controls, how do I identify all the labels apart from
> the button?
Amil - 13 Oct 2004 18:21 GMT
Thanks Imran, you pointed me to the right direction.

> For Each Ctrl As Control In UserControl.Controls
>     If TypeOf Ctrl Is Button Then
[quoted text clipped - 15 lines]
> > loop through all the controls, how do I identify all the labels apart from
> > the button?
Herfried K. Wagner [MVP] - 13 Oct 2004 18:14 GMT
"Amil" <Amil@discussions.microsoft.com> schrieb:
> I have a user control that has a button and five labels.
> Using foreach to loop through all the controls, how do
> I identify all the labels apart from
> the button?

\\\
if (foo is Label)
   ...;
else if (foo is Button)
   ...;
///

Signature

Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/

Amil - 13 Oct 2004 19:11 GMT
How can I loop thru the controls in my usercontrol?  My usercontrol adds a
label everytime you click on the button and there can be none of more than
one labels.  Using foreach requires that the collection is of the same type.

> "Amil" <Amil@discussions.microsoft.com> schrieb:
> > I have a user control that has a button and five labels.
[quoted text clipped - 8 lines]
>     ...;
> ///
Herfried K. Wagner [MVP] - 13 Oct 2004 19:22 GMT
"Amil" <Amil@discussions.microsoft.com> schrieb:
> How can I loop thru the controls in my usercontrol?  My
> usercontrol adds a label everytime you click on the button
> and there can be none of more than one labels.  Using
> foreach requires that the collection is of the same type.

If you are not nesting controls, you can use this code:

\\\
foreach (Control c in this.Controls)
{
   if (c is Label)
   {
       Label cc = (Label)c;
       ...;
   }
   else if (c is Button)
       ...;
}
///

If you set the controls' names, you can uniquely identify the controls...

Signature

Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/

Amil - 13 Oct 2004 20:13 GMT
Thanks Herfried. It works.

> "Amil" <Amil@discussions.microsoft.com> schrieb:
> > How can I loop thru the controls in my usercontrol?  My
[quoted text clipped - 18 lines]
>
> If you set the controls' names, you can uniquely identify the controls...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.