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

Access ALL controls on a form?

Hi,

Is there a way to loop through EVERY control on a form (whether they
are directly on the form, or contained within children of the form)?

I would like to access the size and location of every control - at the
moment im just using a loop as:

For Each c As Control In Me.Controls

For Each cChild As Control In c.Controls

<...>

but this obviously only goes down one level - I could use a whole
series of nested loops, but performance and code readability are quite
important - is there a way to access (or directly loop through) all
the controls?

Thanks

Jamie
Jul 21 '05 #1
2 1378
You could use a recursive method,

The code is in C# but should be easily translated to VB.Net

private ArrayList GetAllChildControls(Control control)
{
ArrayList allControls = new ArrayList();

foreach(Control ctrl in control.Controls)
{
allControls.Add(ctrl);
allControls.AddRange(GetAllChildControls(ctrl))
}

return allControls;
}

To use it simple call

ArrayList allControls = GetAllChildControls(myForm);

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

Jamie Fraser wrote:
Hi,

Is there a way to loop through EVERY control on a form (whether they
are directly on the form, or contained within children of the form)?

I would like to access the size and location of every control - at the
moment im just using a loop as:

For Each c As Control In Me.Controls

For Each cChild As Control In c.Controls

<...>

but this obviously only goes down one level - I could use a whole
series of nested loops, but performance and code readability are quite
important - is there a way to access (or directly loop through) all
the controls?

Thanks

Jamie

Jul 21 '05 #2
Jamie,

See this sample I have sand some days ago in another newsgroup.

I hope this helps?

Cor

\\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
'It starts on a top place, can be any place by instance
'a button event, however for all controls it has to start with Me
End Sub

Private Sub doSet(ByVal thisCtr As Control)
Dim ctr As Control
'a placeholder for the reference of the object is created
For Each ctr In thisCtr.Controls
'The first time each (parent)control on a form
If TypeOf ctr Is Label
'Look if the "Control" is a label
ctr.text = "This you can do for every property from that label
End if
doSet(ctr)
'check if the control has children and do for that the same
Next
End Sub
///

Is there a way to loop through EVERY control on a form (whether they
are directly on the form, or contained within children of the form)?

I would like to access the size and location of every control - at the
moment im just using a loop as:

For Each c As Control In Me.Controls

For Each cChild As Control In c.Controls

<...>

but this obviously only goes down one level - I could use a whole
series of nested loops, but performance and code readability are quite
important - is there a way to access (or directly loop through) all
the controls?

Thanks

Jamie

Jul 21 '05 #3

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

Similar topics

7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
8
by: deko | last post by:
I'm hoping someone can sanity check my understanding of the Object Model for Forms/Controls. I'm having trouble drilling down into Control properties. First, I have a record set with the...
4
by: Wayne Aprato | last post by:
I have a simple database which was originally written in Access 97. When converted to Access 2000 file format it ran flawlessly in Access 2002. I've just tried to run it in Access 2003 and I am...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
6
by: MS | last post by:
Access 97 here. I want a simple way to "lock" certain records on a form. Some records remain "live" until all data is available which happens over time. When all the fields are complete, I want...
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
52
by: Neil | last post by:
We are running an Access 2000 MDB with a SQL 7 back end. Our network guy is upgrading to Windows Server 2003 and wants to upgrade Office and SQL Server at the same time. We're moving to SQL Server...
14
by: ApexData | last post by:
I am considering building some distributable commercial applications. For about a year now, I have been using Access2000. This was my first venture into object oriented database development. ...
9
by: Ron | last post by:
Hi All, I've recently installed a program written in Access 2000 on a laptop. The laptop had an existing Office 2000 Pro which of course included Access. But the program acts oddly (more oddly...
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
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
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
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.