473,323 Members | 1,537 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,323 software developers and data experts.

Controls of a form

Hello

I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained controls
either directly or indirectly through other containers such as panel or
GroupBox controls?

Sep 19 '06 #1
6 1132
Hamed wrote:
Hello

I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained controls
either directly or indirectly through other containers such as panel or
GroupBox controls?
Use a recursive function and start with the form:

private void button1_Click(object sender, EventArgs e)
{
enumerateControls(this.Controls);
}

private void enumerateControls(ControlCollection container)
{
foreach(Control c in container)
{
//Do something with control here

//Now check this control to see if it has any controls of its own
if (c.Controls.Count 0)
enumerateControls(c.Controls);
}
}

Hope this helps and watch for typos

Sep 19 '06 #2
Or for the C# impaired here's the same code in VB syntax

' Air code warning!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
enumerateControls(Me.Controls)
End Sub

Private Sub enumerateControls(ByVal container As ControlCollection)
Dim c As Control
For Each c In container
' Do something with control here

' Now check this control to see if it has any controls of
its own
If c.Controls.Count 0 Then
enumerateControls(c.Controls)
End If
Next
End Sub

Thanks,

Seth Rowe
Chris Dunaway wrote:
Hamed wrote:
Hello

I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained controls
either directly or indirectly through other containers such as panel or
GroupBox controls?

Use a recursive function and start with the form:

private void button1_Click(object sender, EventArgs e)
{
enumerateControls(this.Controls);
}

private void enumerateControls(ControlCollection container)
{
foreach(Control c in container)
{
//Do something with control here

//Now check this control to see if it has any controls of its own
if (c.Controls.Count 0)
enumerateControls(c.Controls);
}
}

Hope this helps and watch for typos
Sep 19 '06 #3
Or for the C# impaired here's the same code in VB syntax

' Air code warning!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
enumerateControls(Me.Controls)
End Sub

Private Sub enumerateControls(ByVal container As ControlCollection)
Dim c As Control
For Each c In container
' Do something with control here

' Now check this control to see if it has any controls of
its own
If c.Controls.Count 0 Then
enumerateControls(c.Controls)
End If
Next
End Sub

Thanks,

Seth Rowe
Chris Dunaway wrote:
Hamed wrote:
Hello

I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained controls
either directly or indirectly through other containers such as panel or
GroupBox controls?

Use a recursive function and start with the form:

private void button1_Click(object sender, EventArgs e)
{
enumerateControls(this.Controls);
}

private void enumerateControls(ControlCollection container)
{
foreach(Control c in container)
{
//Do something with control here

//Now check this control to see if it has any controls of its own
if (c.Controls.Count 0)
enumerateControls(c.Controls);
}
}

Hope this helps and watch for typos
Sep 19 '06 #4
Hi,

Please do not crosspost to so many groups, you have included both C# and VB
ngs.

Post to the group you think will have more in common with your problem and
post to others if not answer is received.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Hamed" <ha***@raymehr.comwrote in message
news:uw****************@TK2MSFTNGP05.phx.gbl...
Hello

I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained
controls either directly or indirectly through other containers such as
panel or GroupBox controls?

Sep 19 '06 #5
Hamed,

You need to traverse the control tree; each control has Controls property
where you can find its children. There is no method that gives you all
controls in a flat list.
--
HTH
Stoitcho Goutsev (100)

"Hamed" <ha***@raymehr.comwrote in message
news:uw****************@TK2MSFTNGP05.phx.gbl...
Hello

I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained
controls either directly or indirectly through other containers such as
panel or GroupBox controls?

Sep 19 '06 #6
Here's an example in C# using an iterator. It'll return all controls in their
front to back order (z-order, not tab-order):

public static IEnumerable<ControlGetAllControls(Control parent)
{
foreach (Control child in parent.Controls)
{
yield return child;
foreach (Control grandChild in GetAllControls(child))
yield return grandChild;
}
}

and you can use it like this:

foreach (Control c in GetAllControls(form))
Trace.WriteLine(c.Name);

Hope this helps.

Regards
Henrik
"Hamed" wrote:
Hello

I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained controls
either directly or indirectly through other containers such as panel or
GroupBox controls?

Sep 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robert | last post by:
Hello. I have been trying out the Lebans ToolTip Classes at http://www.lebans.com/tooltip.htm, to display "balloon" style help tips in a form. The classes I am using are located at...
7
by: Saintor | last post by:
What I do now is I put a value in the tag property, and using the form_current event, I run through all controls properties until the ones with the required tag value are met. Sound OK in theory,...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
3
by: Roger | last post by:
Hi In a Windows forms application I have 2 forms A and B; Form B inherits from form A. Form A is never displayed and its only purpose is to be inherited from and therefore contains mostly...
10
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
22
by: Mr Newbie | last post by:
I was thinking about developing a workflow application yesterday and was musing over the different approaches than one could take in restricting specific actions on a ticket( Form ) at any said...
7
by: Mike Bulava | last post by:
I have created a base form that I plan to use throughout my application let call the form form1. I have Built the project then add another form that inherits from form1, I add a few panel controls...
8
by: Ryan | last post by:
Ok.. I have a form with lots of stuff on it; a tool strip panel, menu strip, data binding elements (dataset, binding source, table adapter), tab control with 7 tab pages, each page contains a...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.