简体   繁体   English

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

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

Suppose I have a label control on a windows form called "UserName". 假设我在一个名为“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... 大多数建议循环,有些人说.NET 2005不支持使用字符串名称直接refenece,而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 ie same drop down menus, labels, react in simlar way to events etc. 我有一个带有三个tabpages的窗体,所有这些设计和功能都非常相似,即相同的下拉菜单,标签,以类似的方式对事件做出反应等。

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()

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 true for the 2nd argument, it will look through the controls of the child controls as well. 如果为第二个参数指定true ,它也将查看子控件的控件。

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. 第二个参数告诉它也搜索所有孩子,所以这肯定会找到你的控件。

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? 你有什么想要完成的事情会让那不对吗?

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. 如果您在运行时添加控件,则可以创建一个字典以包含由字符串索引的引用,并在创建时将每个控件添加到该字典。

One solution would be to pass the controls to the class in addition to or instead of the tabpage. 一种解决方案是将控件传递给类,除了tabpage之外或代替tabpage。 For example, you could have a RecordCounter property in addition to your ActivePage property. 例如,除了ActivePage属性之外,您还可以拥有RecordCounter属性。 (Incidentally, Find is efficient to code but not so efficient at runtime.) (顺便提一下,Find对代码有效,但在运行时效率不高。)

TabPage.Controls.Find("UserName", true) works for me. TabPage.Controls.Find(“UserName”,true)适合我。 But i need to assign it to controls[] class and next i am assigning it to textbox like controls. 但我需要将它分配给controls []类,接下来我将它分配给文本框,就像控件一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM