我如何引用Windows窗体控件的名字(C#/ VB) [英] How do I refer to a windows form control by name (C# / VB)

查看:115
本文介绍了我如何引用Windows窗体控件的名字(C#/ VB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有所谓的用户名Windows窗体上的标签控件。 ?我怎么可以参考该标签编程方式使用标签名称



例如,我可以做的:

 对于TabPage.Controls 
每Ctrl键控制如果ctrl.Name =用户名然后
'做一些
端如果
下一

这似乎是非常低效的。我愿做这样的事情:

  TabPage.Controls(username的)文本=东西

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



修改



感谢迄今的反应。下面是更多的细节。



我有一个窗口有三个的TabPages形成,所有这些一个在设计和功能,即相同的下拉菜单,标签非常相似,反应在simlar方式对事件等。



而不是为我建立了一个控制的事件等等。每个TabPage的一类每TabPage的每一个事件编写代码。



例如,在每个TabPage的有一个叫做RecordCounter,简单地示出了在datagridview的行数时,它是由一个可变的选择在一个填充标签下拉菜单。



所以,我希望能够做的是,一旦在下拉菜单中选择一个变量的选择,在DataGridView填充自身的数据,然后我只是想在一个标签(RecordCounter)来显示的行数。



这也正是每个Tab页相同的过程,使我在做什么是路过Tab页的类,然后我希望能够来指代RecordCounter,然后更新它。



在我的课堂我设置ActivePage属性是TabPage的用户已经选择了,然后希望能够做这样的事情:

  ActivePage.RecordCounter.Text = GetNumberOfRows()


解决方案

您应该能够使用查找()

  TabPage.Controls.Find(用户名,FALSE); 

这将搜索所有的子控件,并返回那些匹配的字符串数组。如果指定真正的第二个参数,它会寻找通过控件的子控件也是如此。


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

解决方案

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.

这篇关于我如何引用Windows窗体控件的名字(C#/ VB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆