How can I reference a Windows form control by name (C # / VB)

Suppose I have a window-shaped label control called "UserName". How can I refer to this tag programmatically using the tag name?

For example, I can do:

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

It seems rather inefficient. I would like to do something like:

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

I did some searches, but could not find a satisfactory answer. Most suggested loops, some say that .NET 2005 does not support direct recovery using a string name, and the FindControl method was only asp.net ...

EDIT

Thanks for the answer. Here are a few details.

I have a window shape with three tabs, all of which are very similar in design and function, that is, drop-down menus, shortcuts, react equally to events, etc.

, , , .. .

, "RecordCounter" , datagridview, .

, , , datagridview , ( "RecordCounter" ,).

, , "RecordCounter" , .

ActivePage TabPage, , , - :

ActivePage.RecordCounter.Text = GetNumberOfRows()
+3
6

Find():

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

, . true , .

+10

?

, : Me.UserName.Text = "Something"

, :

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

, , .

+8

, , . , :

UserName.Text = "Something"

-, , ?

+4

?

, .

, , , , .

+2

. , RecordCounter ActivePage. (, Find , .)

0

TabPage.Controls.Find( "UserName", true) . control [], , .

0

Source: https://habr.com/ru/post/1713472/


All Articles