如何按名称引用 Windows 窗体控件(C#/VB)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1178967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:09:08  来源:igfitidea点击:

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

c#vb.netwinforms

提问by

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

假设我在名为“UserName”的 Windows 窗体上有一个标签控件。如何使用标签名称以编程方式引用该标签?

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...

我做了一些谷歌搜索,但找不到满意的答案。大多数建议循环,有人说 .NET 2005 不支持使用字符串名称的直接引用,而 FindControl 方法仅适用于 asp.net...

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.

例如,在每个标签页上都有一个名为“RecordCounter”的标签,当它通过在下拉菜单中选择一个变量来填充时,它只显示数据网格视图中的行数。

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").

所以我想要做的是,在下拉菜单中选择一个变量后,datagridview 用数据填充自己,然后我只想显示标签中的行数(“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.

这与每个标签页上的过程完全相同,所以我正在做的是将标签页传递给类,然后我希望能够引用“RecordCounter”,然后更新它。

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 属性设置为用户选择的 TabPage,然后希望能够执行以下操作:

ActivePage.RecordCounter.Text = GetNumberOfRows()

回答by Max Schmeling

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"

如果它是在设计时添加的,那么您可以简单地按名称引用它: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.

第二个参数告诉它也搜索所有孩子,所以这肯定会找到你的控件。

回答by David

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?

是否有您正在尝试完成的事情导致它不对?

回答by John Rasch

You should be able to use Find():

您应该能够使用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 truefor the 2nd argument, it will look through the controls of the child controls as well.

这将搜索所有子控件并返回匹配字符串的数组。如果您true为第二个参数指定,它也会查看子控件的控件。

回答by CoderDennis

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.

如果您在运行时添加控件,那么您可以创建一个字典来包含按字符串索引的引用,并在创建时将每个控件添加到该字典中。

回答by xpda

One solution would be to pass the controls to the class in addition to or instead of the tabpage. For example, you could have a RecordCounter property in addition to your ActivePage property. (Incidentally, Find is efficient to code but not so efficient at runtime.)

一种解决方案是将控件传递给类除了标签页之外或代替标签页。例如,除了 ActivePage 属性之外,您还可以有一个 RecordCounter 属性。(顺便说一句,Find 的编码效率很高,但在运行时效率不高。)

回答by ramkumar

TabPage.Controls.Find("UserName", true) works for me. But i need to assign it to controls[] class and next i am assigning it to textbox like controls.

TabPage.Controls.Find("UserName", true) 对我有用。但我需要将它分配给控件 [] 类,接下来我将它分配给控件之类的文本框。