473,412 Members | 2,262 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,412 software developers and data experts.

Find a control on a winform

Hello,

Is there better a way of finding a control on WinForm than to use a
recursive method on the controls collection property? I ask this question
because I have winforms which have many controls on it and the function
takes a long time to beexecuted. This is my function

Public Function FindControl(ByVal oControls As Control.ControlCollection,
ByVal strControlName As String) As Control

Dim MyCtl As Control

For Each MyCtl In oControls
If MyCtl.Name = strControlName Then
Return MyCtl

Else
If MyCtl.Controls.Count > 0 Then
Return FindControl(MyCtl.Controls, strControlName)

End If

End If

Next

End Function

Thank you

--
Marc R.
E-mail
Dim strAddr As String =Convert.ToString(String.Format("{0}.{2}@{3}.{1}",
"mxrc", "cx", "robitxille", "xrs-solutions")).Replace("x", "a")
Nov 21 '05 #1
5 8755
Marc,

What you call long and how many controls.

Cor
Nov 21 '05 #2
5 seconds for more than 300 controls. I know, it is not a good way of having
simple and easy to use forms but it is a requirement of my customer. I have
a form which contains 361 controls. So it is a lot of controls containing a
lot of other controls.

--
Marc R.
E-mail
Dim strAddr As String =Convert.ToString(String.Format("{0}.{2}@{3}.{1}",
"mxrc", "cx", "robitxille", "xrs-solutions")).Replace("x", "a")

"Cor Ligthert [MVP]" <no************@planet.nl> a écrit dans le message de
news: OX*************@TK2MSFTNGP11.phx.gbl...
Marc,

What you call long and how many controls.

Cor

Nov 21 '05 #3
Marc,

I tested your routine in a loop where it was going a million times for three
controls with one inside a panel it took almost no time. Are you sure there
is not another reason.

Cor
Nov 21 '05 #4
Marc,
The recursive find control is probably the "easiest" method to use. However
it is causing performance problems, I would consider using a HashTable to
map control names to controls.

You could either populate the HashTable when you add the controls possibly
by handling the ControlAdded & ControlRemoved events (which may be difficult
if you have controls nested within controls). Note ControlAdded &
ControlRemoved are very advanced events & are normally hidden.

Or if you could simply call a recursive routine, similar to your
FindControl, that populates the hashtable when you know that all controls
have added. Such as the end of the Form.Load event or after you call a
routine that adds a number of dynamic controls...

http://msdn.microsoft.com/library/de...addedtopic.asp

http://msdn.microsoft.com/library/de...movedtopic.asp

Hope this helps
Jay
"Marc Robitaille" <ma*************@ars-solutions.caa> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
| Hello,
|
| Is there better a way of finding a control on WinForm than to use a
| recursive method on the controls collection property? I ask this question
| because I have winforms which have many controls on it and the function
| takes a long time to beexecuted. This is my function
|
| Public Function FindControl(ByVal oControls As Control.ControlCollection,
| ByVal strControlName As String) As Control
|
| Dim MyCtl As Control
|
| For Each MyCtl In oControls
| If MyCtl.Name = strControlName Then
| Return MyCtl
|
| Else
| If MyCtl.Controls.Count > 0 Then
| Return FindControl(MyCtl.Controls, strControlName)
|
| End If
|
| End If
|
| Next
|
| End Function
|
| Thank you
|
| --
| Marc R.
| E-mail
| Dim strAddr As String =Convert.ToString(String.Format("{0}.{2}@{3}.{1}",
| "mxrc", "cx", "robitxille", "xrs-solutions")).Replace("x", "a")
|
|
Nov 21 '05 #5
"Marc Robitaille" <ma*************@ars-solutions.caa> schrieb:
Is there better a way of finding a control on WinForm than to use a
recursive method on the controls collection property?


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #6

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

Similar topics

3
by: Z D | last post by:
Hello, I've created a winform user control that, at some point in the default constructor, looks for a specifc file. When I try to load the user control to my winform's form during design time...
5
by: | last post by:
I'm having difficulty embedding a managed C++ control in Internet Explorer. I'm using the same object syntax in the html as I would for a c# control, but IE doesn't seem to find the control in the...
8
by: Sunil Menon | last post by:
Dear All, We are developing applications in ASP.Net...in one of our applications we would like to use a GridControl...we have tried to use a Server-Side Grid control but found the speed to be an...
5
by: Drew | last post by:
I have a user control written in C#. This user control uses some of my mixed mode C++ libraries. My user control is currently residing on a .NET Windows Form. Everything works nicely. Now I am...
3
by: Eric | last post by:
If I have a windows form control declared in my aspx page: <OBJECT id="wcControl" classid="http:MyControl.dll#MyControls.wcControl" height="100%" width="100%" VIEWASTEXT> </OBJECT> Can I not...
3
by: EJ1003 | last post by:
Hello I would like to create Activex Control uisng C# and use it in ASP.Net webform. User Control is not solving my requirement so I am going for Activex Control. Please guide me on this, how...
4
by: Scott M. | last post by:
I need to be able to have a web form that users can enter (either by typing or pasting from a rich text format) text into. The entered text will then be saved as a new web page (.htm). The web...
2
by: moondaddy | last post by:
I had to repost this because I had to update and change my msdn alias. I will re-ask the question and clarify a few things that were not clear before. This code is all executed on my dev...
3
by: Steve | last post by:
Hi All I am using VB.net 2008 and use timer controls within my applications Question Does the code in a Timer control.tick event run on a different thread to the main Application thread (UI...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.