在Windows窗体上查找控件 [英] Finding controls on a Windows's form

查看:100
本文介绍了在Windows窗体上查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在GroupBox控件中有一个Button控件,而该控件又在Tab控件中.
现在,我想动态地找到那个Button控件.
为此,是否有必要遍历所有容器控件,然后找到Button控件?
我阅读了此文章 [ 我不想为所有15个按钮编写相同的代码.
我只是将按钮名称传递给该函数并更改其文本和可见性.
并非全部15个按钮都是必需的,而是基于特定条件.
<-update>


谢谢,
Nagendra.

Hello All,

I have a Button control inside a GroupBox controls which is again inside a Tab control.
Now, i want to dynamically find that Button control.
For this, is it necessary to iterate through all container controls and then find Button controls?
I read this article[^] on CodeProject in which author has mentioned that we need to iterate.
Is there any new functionality available in c#4.0 using which i can directly find Button control without iterating through all container contols?

<--update-->
Actually, i have 15 buttons on my form and i am creating a Control Array of it just like in VB, so that all buttons have a common Click Event.
So for this manipulation, i need to find control dynamically, cast it to a Button control and use its reference.
I am trying to find the control using its name only.
<--update-->


<--update>
I want to change Text and Visibility of my all 15 buttons, for this i need to find button by passing its name to a function.
I don''t want to write same code for all 15 buttons.
I am just passing button name to the function and change its text and visibility.
Not necessariliy for all 15 buttons but based on certain condition.
<--update>


Thanks,
Nagendra.

推荐答案

您可以使用它的名称...您在设计器中给它的名称...

或者,如果您动态添加了按钮,则可以在创建按钮时将其保存在类变量中.


实际上,我在窗体上有15个按钮,并且像在VB中一样,我正在为其创建一个控件数组,以便所有按钮都具有一个通用的Click事件.
因此,对于这种操作,我需要动态地找到控件,将其转换为Button控件并使用其引用.
我正在尝试仅使用其名称来查找控件."


哦!这甚至更容易:每个事件都包含一个"sender"参数,该参数是引发该事件的控件.只需投射它,您就可以离开了:
You could just use it''s name...The one you gave it in the designer...

Or, if you dynamically added the button, you could save it in a class variable when you create it.


"Actually, i have 15 buttons on my form and i am creating a Control Array of it just like in VB, so that all buttons have a common Click Event.
So for this manipulation, i need to find control dynamically, cast it to a Button control and use its reference.
I am trying to find the control using its name only."


Oh! That''s even easier: every event includes a "sender" parameter which is the control that raised the event. Just cast it and you are off:
private void button1_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        ...
        }
    }


动态查找您自己的Form控件的任务是设计错误的一个好兆头.

您引用的代码不是很好(请参阅注释中的一些批评),但是这个想法行得通.这里的主要问题是:为什么要通过属性Name找到它?没有什么可以保证它在表格范围内是唯一的.您可以创建和抽象谓词(例如,以方法的形式)并将此谓词作为委托实例传递.特别是,您可以结合使用控件的Type(使用"is"运算符)和其他控件(再次使用Name).

与v.3.5相比,v.4.0中没有什么可以帮助您的新功能.如果与v.2.0进行比较,则新功能是 lambda表达式,它可以帮助您缩短用于谓词(代理实例)的匿名方法的语法.这确实是次要功能;您可以在v.2.0中使用delegate关键字和语法使用匿名方法.

更重要的是使用替代设计.如果您能解释找到控件的最终目的,也许我就能将您引向正确的方向.

[更新]

基于OP的澄清的其他问题:

仅为按钮编写一些代码对于搜索来说是一个很差的理由(即使搜索可以工作,但这完全没有意义).

您需要根据名称应用不同的条件.为什么? (我用名称解释了这个问题.)您可以在编译时使用实例本身.在编译期间,您具有所有实例作为表单"字段.您不会使用名称来缩短代码.而是在M按钮N < M中创建N按钮数组.通过用于设置按钮的代码对这些按钮进行分组-需要相同代码的按钮进入同一阵列.循环遍历每个数组的代码.

另一种方法:15个按钮对于Designer来说已经太多了!它使您的代码无法维护.将它们全部从设计器中删除,仅设计面板和布局.在运行时添加按钮(使用MyPanel.Controls.Add),设计适当的初始化,计算数组中每个Button的坐标.最后,从Form构造函数调用安装方法.我更喜欢这种方法.

同样,在同一个表格中有如此多的按钮会使您的用户发疯.超过5-6的数字太多了.考虑将所有按钮(某些执行命令"按钮)与命令的ListBox合并.根据选择还有许多其他设计:TreeViewListViewDataGrid等.菜单,毕竟.许多按钮是不良设计的标志.看一下Visual Studio.您可以看到多少个按钮?这是相当不错的设计.

—SA
The task of dynamically finding of a control of your own Form is a good sign of wrong design.

The code you reference is not very good (see some criticism in comments), but the idea is working. Major problem here is: why finding it by the property Name? Nothing guarantees it is unique in the scope of the Form. You can create and abstract predicate (int the form of method, for example) and pass this predicate as a delegate instance. In particular, you can use combination of the Control''s Type (using "is" operator) and something else (Name again).

There is nothing new in v.4.0 which could help you compared to v.3.5. If you compare with v.2.0, the new feature is lambda expression with would help you to shorted up syntax of the anonymous method you would use for the predicate (delegate instance). This is really minor feature; you can use anonymous method in v.2.0 using delegate keyword and syntax.

More important thing would be using alternative design. If you can explain your ultimate goal of finding of the control, perhaps I would be able to direct you in right direction.

[UPDATE]

Additional concerns based on OP''s clarification:

Just writing some code for a button is a very poor justification for the search (even though the search will work, it is completely pointless).

You need to apply different criteria based on names. Why? (I explained the issue with names.) You can use instances themselves in compile time. You have all instances as the Form field during compile time. You will not shorten your code using names. Instead, create N Button arrays our of M buttons, N < M. Group those buttons by the code used to setup them — the Buttons requiring identical code goes to the same array. Loop you code through each array.

Another approach: 15 buttons is already too many for Designer! It makes your code not maintainable. Remove them all from the designer, design just panels and layout. Add the buttons during run-time (use MyPanel.Controls.Add), design proper initialization, calculate coordinates of each Button in the array. Call your setup method from you Form constructor, at the very end. I would prefer this method.

Also, having so many buttons at the same form will drive your user crazy. Anything more than 5-6 is too many. Consider merging all buttons in one (some "Execute command" button) combined with the ListBox of commands. There are many other designs based on selection: TreeView, ListView, DataGrid and more. Menu, after all. Many buttons is a sign of bad design. Look at Visual Studio. How many buttons can you see? And this is quite good design.

—SA



尝试以下方法将所有控件都置于Winform中.

Hi,
Try the following methods to get all the control in a winform.

public static List<Control> GetControls(Control form)
{
    var controlList = new List<Control>();
    foreach (Control childControl in form.Controls)
    {
        // Recurse child controls.
        controlList.AddRange(GetControls(childControl));
        controlList.Add(childControl);
    }
    return controlList;
}


这篇关于在Windows窗体上查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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