How to find all the instances of a control in my windows applicati

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

So, I have a Windows Forms application in .NET 2.0. I also have a custom
UserControl. How do I find all the instances of my UserControl in an
Application?

I've just tried the new Application.OpenForms with a loop that searches all
the controls in the opened forms, but my program just crushes when it
attempts it (it just crushes along with VisualBasic.NET Express 2005 with no
error).
 
Hello man,
don't know about the IDE crash, but i can show you a recursive method tha
look in the controls of the form, IMPORTANT, if you have i groupBox and
inside it you place your control, a loop in the Controls property of the
form WILL NOT give you your userControl so insdead use this functions:

private void findControl (){

Control returned = null;

//looping throught the controls of the form

foreach (Control cont in this.Controls){

returned = GetControl (cont);

//if the function returns a control then break the loop.

if (returned != null)

break;

}

}

private Control GetControl (Control c){

//setup the returned control

Control returnedControl = null;

//if the control have mor controls, loop and call the same Function
(recursive)

if (c.Controls.Count>0){

foreach (Control cont in c.Controls){

returnedControl = GetControl (cont);

if (returnedControl != null)

break;

}

}else{

//if the control is the one you looking for break the loop and return the
control

if (c.Name == "YourControlName")

returnedControl = c;

}

return returnedControl;

}



Hope that help

Nassos
 
Thx for the response.
I've tried what you said before.
But at last I used another way. I used a shared array: Each time a control
is created, it adds itself to the array and each time a control is disposed,
it removes itself.

Thx for the tip
Nice to see you're also greek!
 
Back
Top