473,379 Members | 1,220 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,379 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 1022
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...
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: 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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.