【问题标题】:How to have multiple panels with different controls in a form in C#如何在 C# 的表单中拥有多个具有不同控件的面板
【发布时间】:2012-06-14 18:41:07
【问题描述】:

我是 C#.Net 的新手。 我有一个表格,里面有一些面板。其中一个面板是 MainPanel。启动后,MainPanel 为空。根据用户选择,我想在其中加载一些控件。类似于 Java 中的 CardLayout 的东西!每个面板都有很多控件,我不想以编程方式添加它们。事实上,问题是“有没有办法让设计师设计一些面板并根据用户选择显示/隐藏它们,全部以一种形式?”

谢谢。

【问题讨论】:

  • 这是 Windows 窗体、WPF、Silverlight、ASP.Net 吗?
  • 他标记了 Winforms,所以很可能是这样。
  • 我添加了一个屏幕截图,可以帮助我表达我的意思。
  • 那么上面有黄色方块的部分,那个家伙图片下面的列,以及主要部分?是的,它们都可以作为用户控件。将它们放在自己的类中,然后您可以在设计器模式下从工具箱中添加它们,并将它们按您想要的方式放置在表单上。另一种让生活更轻松的方法是将面板(或拆分容器)放入表单中,以便您可以布局每个面板的大小。然后将 UserControls 添加到 Panel.Controls 属性中,或者将它们放入 Designer 中。
  • 左下角的空白是MainPanel。我想根据用户在顶部栏按钮中的选择来更改它,并使用之前设计的其他面板并准备好使用。

标签: c# .net winforms


【解决方案1】:

是的,在它们自己的类中创建名为 UserControls 的新对象。您可以通过编程方式添加和删除它们,但可以在设计器中创建它们。

为避免在更改控件时出现闪烁,请执行以下操作:

Control ctlOld = frmMain.Controls[0]; // this will allow you to remove whatever control is in there, allowing you to keep your code generic.
ctlNextControl ctl = new ctlNextControl(); // this is the control you've created in another class
frmMain.Controls.Add(ctlNextControl);
frmMain.Controls.Remove(ctlOld);

创建您的用户控件,并为它们命名,我现在将命名一些示例:

ctlGear
ctlMap
ctlGraph
ctlCar
ctlPerson

将这 5 个文件作为用户控件添加到您的项目中。随心所欲地设计它们。

为不同的按钮创建一个枚举以便于使用:

public enum ControlType {
    Gear,
    Map,
    Graph,
    Car,
    Person
}

创建它们后,在每个按钮的按钮单击事件中,添加对这个新方法的调用:

private void SwitchControls(ControlType pType) {
    // Keep a reference to whichever control is currently in MainPanel.
    Control ctlOld = MainPanel.Controls[0];
    // Create a new Control object
    Control ctlNew = null;
    // Make a switch statement to find the correct type of Control to create.
    switch (pType) {
        case (ControlType.Gear):
           ctlNew = new ctlGear();
           break;
        case (ControlType.Map):
           ctlNew = new ctlMap();
           break;
        case (ControlType.Graph):
           ctlNew = new ctlGraph();
           break;
        case (ControlType.Car):
            ctlNew = new ctlCar();
            break;
        case (ControlType.Person):
            ctlNew = new ctlPerson();
            break;
        // don't worry about a default, unless you have one you would want to be the default.
    }

    // Don't try to add a null Control.
    if (ctlNew == null) return();

    MainPanel.Controls.Add(ctlNew);

    MainPanel.Controls.Remove(ctlOld);
}

然后在你的按钮点击事件中,你可以有这样的东西:

private void btnGear.Click(object sender, EventArgs e) {
    SwitchControls(ControlType.Gear);
}

其他的点击事件也是一样,只是把参数中的ControlType改了。

【讨论】:

    【解决方案2】:

    只需在其他面板上设置Visible 属性。但是,LarsTech 是对的,最好根据需要将用户控件切换到主面板

    【讨论】:

      【解决方案3】:

      尝试将这些其他“面板”创建为用户控件。您可以像设计表格一样设计它们。

      或者,您可以使用这个 Hans Passant 答案,他使用 TabControl 但在运行时隐藏选项卡:Creating Wizards for Windows Forms in C#

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-22
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多