如何在 Windows 窗体中动态加载面板?

发布于 2024-08-23 14:19:22 字数 144 浏览 7 评论 0原文

winform顶部有一些按钮,当我单击其中一个按钮时,下面的面板将加载不同的预定义面板,我该如何实现呢?

请参阅此示例:

在此处输入图像描述

there are some buttons on the top of the winform, and when I click one of them, the panel below will load different predefined panel, how can I implement this ?

please see this example:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这是使用标准 WinForms TabControl 的解决方案,其中选项卡在运行时隐藏,但当然它们在设计时可用。

假设:

  1. 您不想创建 OwnerDrawn 选项卡,这是可能的。

  2. 标准的 WinForms TabControl 将满足您所有的设计时需求。

代码:

  1. 在托管 TabControl 的表单的表单加载事件中,使用如下代码:

    tabControl1.Region = new Region(tabControl1.DisplayRectangle);
    

    隐藏选项卡。

  2. 然后,“连接”按钮以处理在 TabControl 中选择不同的 TabPage。显然你可以用比这更优雅的方式来做到这一点:

    private void button1_Click(对象发送者,EventArgs e)
    {
      tabControl1.SelectedTab = tabControl1.TabPages[0];
    }
    
    私人无效button2_Click(对象发送者,EventArgs e)
    {
      tabControl1.SelectedTab = tabControl1.TabPages[1];
    }
    

注意:如果您想将辅助表单或用户控件插入到 TabControl 的 TabPage 中:这不是问题:当然使用 UserControl 更简单。将它们插入每个 TabPage 的 Controls 集合中,并将其“Dock 属性”设置为“DockStyle.Fill”。

注意:有一些更奇特的方法可以隐藏选项卡,例如使用派生的 TabControl,如 CodeProject 上所示:WinForm 上的 TabControl 不显示 Tab 标题? 还有其他使用修改后的 WndProc 的解决方案。它们并不难找到。

Here's a solution using a standard WinForms TabControl, where the Tabs are hidden at run-time, but of course they are available at design-time.

Assumptions :

  1. You don't want to get into creating OwnerDrawn Tabs, which is possible.

  2. A standard WinForms TabControl will meet all your design-time needs.

Code :

  1. In the Form Load event of the Form that hosts your TabControl use code like this :

    tabControl1.Region = new Region(tabControl1.DisplayRectangle);
    

    To hide the Tabs.

  2. Then, "wire" up your buttons to handle selecting the different TabPages in the TabControl. Obviously you could do this in a more elegant way than this :

    private void button1_Click(object sender, EventArgs e)
    {
      tabControl1.SelectedTab = tabControl1.TabPages[0];
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
      tabControl1.SelectedTab = tabControl1.TabPages[1];
    }
    

Note : if you want to insert secondary Forms or UserControls into the TabPages of a TabControl : that's not a problem : of course simpler to use UserControls. Insert them into the Controls collection of each TabPage and set their 'Dock Property to 'DockStyle.Fill.

Note : there are fancier ways you can hide the Tabs, like using a derived TabControl as shown here on CodeProject : TabControl on a WinForm without showing the Tab header? There are other solutions out there that use a modified WndProc. They're not hard to find.

掩于岁月 2024-08-30 14:19:22

我不知道你到底想做什么,但是如果你的表单上有一个名为 contentArea 的面板,并且创建了一堆用户控件(但不在表单上),那么您可以使用它作为按钮的事件处理程序:

public void myButton_Click(object sender, EventArgs e) {
    contentArea.Controls.RemoveAt(0);
    contentArea.Controls.Add(new MyUserControl());
}

...尽管正如其他人所说,在这种情况下选项卡控件会更好。

I don't know exactly what you're trying to do, but if you've got a Panel on your form named contentArea and a bunch of user controls created (but not on the form), then you could use this as an event handler for a button:

public void myButton_Click(object sender, EventArgs e) {
    contentArea.Controls.RemoveAt(0);
    contentArea.Controls.Add(new MyUserControl());
}

...though as other people have said, a tab control would be better in this case.

南冥有猫 2024-08-30 14:19:22

您可以做的是将它们分别放在单独的面板中。将每个的 Visible 属性设置为 false。当按钮上发生 Click 事件时,将所有按钮的 Visible 属性设置为 false 并设置您想要显示的 Visibletrue

What you can do is have them each in a separate Panel. Set the Visible property to false to each. When the Click event on the button, set the Visible property of all of them to false and set the one you want shown's Visible to true.

你的背包 2024-08-30 14:19:22

例如,如果您有两个表单 Form1 和 Form2,并且您想在 from1 中加载 form2。当您按下按钮加载form2时,代码如下所示,

private void button1_Click(object sender, EventArgs e)
{
     Form2 form2 = new Form2();
     this.Controls.Clear();
     foreach(Control c in this.Controls)
     {
         this.Controls.Add(c);
     }
}

此代码会将form2中的所有控件加载到form1中。

For example if you have two form Form1 and Form2 and you want to load form2 inside from1. when you press a button to load form2 the code is like this

private void button1_Click(object sender, EventArgs e)
{
     Form2 form2 = new Form2();
     this.Controls.Clear();
     foreach(Control c in this.Controls)
     {
         this.Controls.Add(c);
     }
}

this code will load all the controls in the form2 into form1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文