如何查找表单中使用了多少控件 [英] how to find how many controls are used in the form

查看:108
本文介绍了如何查找表单中使用了多少控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设计具有不同控件的窗体。单击按钮,消息显示该表单中使用了多少控件,以及他的名称和类型以显示......如何...?

design a windows form with different controls. Click the button the message shows how many controls are used in that form, and his name and type to show... How...?

推荐答案

1月, 2014年,我发布了一个CP QA问题的答案,该问题显示了如何使用堆栈获取所有控件,而不是使用通常的递归技术:[ ^ ]。



这个答案有完整的源代码,例子,以及我从中得到这个想法的链接,以及关于使用基于Stack的技术的讨论的链接,这些技术比我将要聪明得多。



修改此代码以构建找到的每个控件类型的记录,并且可以像这样执行每种控件类型的实例列表:
In January, 2014, I posted an answer to a CP QA question that shows how to get all the Controls using a 'Stack, rather than using the usual recursive technique: [^].

That answer has complete source code, and examples, and links to where I got the idea from, and links to discussion of using the Stack based technique by people much smarter than I will ever be.

Modifying this code to build a record of each Control Type found, and a List of instances of each Type of Control can be done like this:
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

public static class SpecialMethods
{
    // added 12/15/2014 by bw
    public static Dictionary<string,List<Control>> GetControlsByType(Control aControl)
    {
        // see comments
        return GetAllControls(aControl)
            .GroupBy(typ => typ.GetType().Name)
            .Select(grp => grp.ToList())
            .ToDictionary(name => name[0].GetType().Name);
    }

    public static IEnumerable<Control> GetAllControls(Control aControl)
    {
        Stack<Control> stack = new Stack<Control>();
 
        stack.Push(aControl);
 
        while (stack.Any())
        {
            var nextControl = stack.Pop();
 
            foreach (Control childControl in nextControl.Controls) stack.Push(childControl);

            yield return nextControl;
        }
    }
}

现在我们可以使用Form(或任何其他可能具有Child Controls的Control)来测试它:

Now we can test this using a Form (or any other Control that may have Child Controls):

private void Form1_Load(object sender, EventArgs e)
{
    foreach (KeyValuePair<string,List<Control>> kvp in SpecialMethods.GetControlsByType(this))
    {
        Console.WriteLine("Control Type: {0} Count: {1}", kvp.Key, kvp.Value.Count.ToString());
    }
}

以下是测试的示例输出:

Here's a sample output from a test:

Control Type: Form1 Count: 1
Control Type: ToolStrip Count: 2
Control Type: Button Count: 4
Control Type: TextBox Count: 1
Control Type: Label Count: 3
Control Type: GroupBox Count: 1
Control Type: Panel Count: 2

评论:



'GetControlsByType静态方法中发生了什么:



1.根据每个控件类型的名称将所有控件过滤为一组IGroupings:通过GroupBy操作



2每个IGrouping被转换成List< Control>。通过'选择操作。



3.创建一个字典,其密钥设置为每个列表中第一个控件的类型名称< Control>在每个组中。

Comments:

What's happening in the 'GetControlsByType static method:

1. all the Controls are filtered into a set of IGroupings based on the name of the Type of each Control: by the GroupBy operation

2. each IGrouping is transformed into a List<Control> by the 'Select operation.

3. a Dictionary is created with the key set to the Name of the Type of the first Control in each List<Control> in each Group.


遍历Form.Controls集合 - 它包含表单具有的所有控件。您可能需要递归:某些控件(如Panel)也可以包含自己的Controls集合。
Iterate through the Form.Controls collection - it contains all the controls the form has. You will probably need to be recursive: some controls (such as Panel) can contain their own Controls collection as well.


这篇关于如何查找表单中使用了多少控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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