How do I refer to a windows form control by name (C# / VB)

18,386

Solution 1

You should be able to use Find():

TabPage.Controls.Find("UserName", false);

This will search all of the child controls and return an array of those that match the string. If you specify true for the 2nd argument, it will look through the controls of the child controls as well.

Solution 2

Is your control added at design time or runtime?

If it is added at design time then you can simply reference it by name: Me.UserName.Text = "Something"

If it is added at runtime you can use the following:

TabPage.Controls.Find("UserName", true) 

The second parameter tells it to search all children as well, so this should definitely find your control.

Solution 3

Unless I'm misunderstanding the question context, I think you're making it too difficult. in code, you just use thelabel name as your reference as in:

UserName.Text = "Something"

Is there something you're trying to accomplish that makes that not right?

Solution 4

How are you creating your controls?

If it's at design time, then each control should have a named reference.

If you're adding controls at run time, then you could create a dictionary to contain references indexed by string and add each control to that dictionary when it's created.

Share:
18,386
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    Suppose I have a label control on a windows form called "UserName". How can I refer to that label programmatically using the label name?

    For example I can do:

    For each ctrl as Control in TabPage.Controls
    If ctrl.Name = "UserName" Then
    ' Do something
    End If
    Next
    

    This seems quite inefficient. I would like to do something like:

    TabPage.Controls("UserName").Text = "Something"
    

    I did some googling but couldn't find a satisfactory answer. Most suggested looping, some said .NET 2005 doesn't support direct refenece using string name, and FindControl method was asp.net only...

    EDIT

    Thanks for the response so far. Here is a bit more detail.

    I have a windows form with three tabpages, all of which a very similar in design and function i.e. same drop down menus, labels, react in simlar way to events etc.

    Rather than write code for each event per tabpage I have built a class that controls the events etc. per tabpage.

    For example, on each tabpage there is a Label called "RecordCounter" that simply shows the number of rows in the datagridview when it is populated by selection of a variable in a drop down menu.

    So what I want to be able to do is, upon selection of a variable in the drop down menu, the datagridview populates itself with data, and then I simply want to display the number of rows in a label ("RecordCounter").

    This is exactly the same process on each tabpage so what I am doing is passing the tabpage to the class and then I want to be able to refer to the "RecordCounter" and then update it.

    In my class I set the ActivePage property to be the TabPage that the user has selected and then want to be able to do something like:

    ActivePage.RecordCounter.Text = GetNumberOfRows()
    
  • MarkJ
    MarkJ almost 15 years
    +1, I was just typing my own answer. Here's a link to the MSDN docs on Find msdn.microsoft.com/en-us/library/…