0

I'm trying to refer to a control by a variable name. The control is located in a panel within a tab control.

I can refer to a control if it's on the parent form like this:

Me.Controls("TextBoxName").Text = "test text"

Is there an easy way of referring to any control regardless of where it is?

Failing that, how do I loop through all the controls to find it? - I'm not sure how to get a control that's on a tab.

Thanks!

Ash
  • 23
  • 4
  • [Control.ControlCollection.Find](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.controlcollection.find?view=net-5.0) – dr.null Feb 15 '21 at 16:42
  • Does this answer your question? [loop over all textboxes in a form, including those inside a groupbox](https://stackoverflow.com/questions/4673950/loop-over-all-textboxes-in-a-form-including-those-inside-a-groupbox) – Olivier Jacot-Descombes Feb 15 '21 at 17:05
  • Thanks for reply - unfortunately I'm just had to drop everything as something urgent has come up. I'll try out these suggestions in the morning. Really appreciate the quick response and wish I had time to look at them right now! – Ash Feb 15 '21 at 17:15
  • If you already have the Tab control on the page you are on, you can access it's child controls. For example, if this control ```````` was on your page, in the code-behind you could do ````Dim txtBox as TextBox = Ctype(Me.MyUserTabControl.FindControl("TextBoxName"), TextBox)```` and then access it from the parent page that way. – JohnPete22 Feb 15 '21 at 18:01
  • 1
    Yogesh/dr.null's answer worked perfectly - thanks guys, it's very much appreciated! – Ash Feb 16 '21 at 10:27

1 Answers1

1

Here as per @dr.null refered you, You can just find control by putting control name directly

Here is Code for your reference

 Dim Controls() As Control

        Controls= Me.Controls.Find("TextBox2", True)

        If Not IsNothing(Controls) Then
            For Each cntrl As Control In Controls
                cntrl.Text = "text here "
            Next

        End If

Here you do not need to check panel name as windows form does not allow to enter duplicate control name

Yogesh
  • 180
  • 1
  • 10