简体   繁体   English

使用基本表单生成器构建用于C#管理的面板列表

[英]Build list of Panels for management in C# with Rudimentary Form Builder

I'm working in a form builder that allows me to input select custom code. 我正在使用一个允许我输入选择自定义代码的表单生成器。 I'm trying to build a rudimentary window manager that shows and hide's panels. 我正在尝试构建一个显示和隐藏面板的基本窗口管理器。 I'm using .Visible and System.Drawing.Point in this form OnClick: 我使用的是OnClick形式的.Visible和System.Drawing.Point:

public void togglePanel(Panel panel)
{
    if(panel.Visible) 
    {
        panel.Visible = false;
    }
    else 
    {
        panel.Visible = true;
        panel.Position = new System.Drawing.Point(panel1.Right + 5, panel1.Top);
    }
}

Currently closeAllPanels is just a long list of declarations, ie panel2.Visible = false; 当前closeAllPanels只是一长串声明,即panel2.Visible = false;

How can I generate a list of these panels? 如何生成这些面板的列表? Can I do so using the GetMembers() method? 我可以使用GetMembers()方法吗? I'm new to C#, so I'm not really sure which class I'd need to run GetMembers() on to generate that list. 我是C#的新手,所以我不太确定我需要在哪个类上运行GetMembers()才能生成该列表。

Or is there a much easier way that I'm completely missing? 还是有一种我更容易错过的简便方法?

If all of the Panels will always be in the same container, then do something like: 如果所有面板都始终放在同一个容器中,请执行以下操作:

        List<Panel> Panels = new List<Panel>(this.Controls.OfType<Panel>());
        Console.WriteLine("# of Panels: " + Panels.Count.ToString());
        foreach(Panel pnl in Panels)
        {
            Console.WriteLine(pnl.Name + ": Visible = " + pnl.Visible.ToString());
        } 

You can replace "this" with the name of the container if the Panels are not directly contained by the Form itself. 如果面板本身不直接包含面板,则可以用容器名称替换“ this”。

To get a reference to a Panel "by name" with Controls.Find(): 要使用Controls.Find()来引用面板“按名称”:

        string panelName = "panel1";
        Control[] matches = this.Controls.Find(panelName, true);
        if (matches.Length > 0 && matches[0] is Panel)
        {
            Panel pnl = (Panel)matches[0];
            // ... do something with "pnl" ...
        }

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

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