Find controls on a windows form

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

Guest

Hi,

I have a windows form with several controls on it. I need a function which
finds a specific control on this form. Is there any function for that already
built? If not, could you advice me how to tackle this problem?

Thanking you in advance,
Emilia
 
Walk through the Controls collection of the form

Control FindControl(string name)
{
Control ret = null;
foreach( Control c in Controls )
{
if( c.Name == name )
{
ret = c;
break;
}
}
return ret;
}

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hi,

I have a windows form with several controls on it. I need a function which
finds a specific control on this form. Is there any function for that already
built? If not, could you advice me how to tackle this problem?

Thanking you in advance,
Emilia
 
Hi,
You can use the controls collection. Eg:
// C#
foreach (Control c in this.Controls)
{
if (c == txtFirstName) // Name of the control to be found
{
((TextBox)c).Text = "My First Name";
break;
}
}

HTH.

Hi,

I have a windows form with several controls on it. I need a function which
finds a specific control on this form. Is there any function for that
already
built? If not, could you advice me how to tackle this problem?

Thanking you in advance,
Emilia
 
I dont think you can use Contains since it needs a Control reference to be
passed as input.
??
 
Hi Girish,

Well, I though as she requires a specific control, she has the reference.
Didn't know what exactly she required - getting the control by name or so.

Regards,
Rakesh Rajan
 
Hi,

Thank you very much for your suggestions. I have tried the following:

Private Function FindControl(ByVal name_ctrl As String, ByVal control As
Control)
Dim ctrl As Control

For Each ctrl In control.Controls
If (StrComp(ctrl.Name, name_ctrl, ComparedMethod.Text) = 0) Then
Return ctrl
Exit Function
ElseIf ctrl.HasChildren Then
FindControl(name_ctrl, ctrl)
End If
Next
End Function

But it is not working. It returns nothing……… Do you know why??????
 
Have "Return FindControl (name_ctrl, ctrl)" instead of just FindControl(...)
in the ElseIf part...

Hi,

Thank you very much for your suggestions. I have tried the following:

Private Function FindControl(ByVal name_ctrl As String, ByVal control As
Control)
Dim ctrl As Control

For Each ctrl In control.Controls
If (StrComp(ctrl.Name, name_ctrl, ComparedMethod.Text) = 0) Then
Return ctrl
Exit Function
ElseIf ctrl.HasChildren Then
FindControl(name_ctrl, ctrl)
End If
Next
End Function

But it is not working. It returns nothing... Do you know why??????
 
I tried your code and seems to be working fine though. This is what I ran:

-- Code Start --
Private Function FindControl(ByVal name_ctrl As String, ByVal control As
Control)
Dim ctrl As control
For Each ctrl In control.Controls
If (StrComp(ctrl.Name, name_ctrl, vbTextCompare) = 0) Then
Return ctrl
Exit Function
ElseIf ctrl.HasChildren Then
Return FindControl(name_ctrl, ctrl)
End If
Next
End Function
-- Code End --

I called it as FindControl ("button5", Me) and it returned the button
control instance with name button5. When passed a non-existent control name,
it returned Nothing.

Let me know if I am missing anything.

I have tried it, but it does not work. Any other idea?
 
The problem comes when the control has children. For example, in my case I am
looking for a TextBox that is contained by Form -> TabPage -> GroupBox ->
TextBox.
 
Well, I tested this scenario (Form having Tab having GroupBox having
TextBox!!) as well and it indeed returns the correct instance of the text
box when called FindControl (<textbox name>, Me)...

The problem comes when the control has children. For example, in my case I
am
looking for a TextBox that is contained by Form -> TabPage -> GroupBox ->
TextBox.
 
Back
Top