winform页面布局
时间: 2024-01-14 18:03:21 浏览: 33
在Winform中,页面布局可以使用多种方式来实现。以下是两种常见的布局方式:
1. 使用控件"WeifenLuo.WinFormsUI.Docking"来承载窗体界面内容:
```csharp
using WeifenLuo.WinFormsUI.Docking;
public partial class MainForm : Form
{
private DockPanel dockPanel;
public MainForm()
{
InitializeComponent();
dockPanel = new DockPanel();
Controls.Add(dockPanel);
}
private void MainForm_Load(object sender, EventArgs e)
{
// 创建并添加需要承载的窗体界面内容
MyForm1 form1 = new MyForm1();
form1.Show(dockPanel, DockState.Document);
MyForm2 form2 = new MyForm2();
form2.Show(dockPanel, DockState.Document);
}
}
```
2. 使用多文档容器界面或基于用户控件的变化实现同一个面板显示不同的控件内容:
```csharp
public partial class MainForm : Form
{
private Panel contentPanel;
public MainForm()
{
InitializeComponent();
contentPanel = new Panel();
Controls.Add(contentPanel);
}
private void MainForm_Load(object sender, EventArgs e)
{
// 创建需要显示的控件内容
UserControl1 userControl1 = new UserControl1();
UserControl2 userControl2 = new UserControl2();
// 初始时显示控件1
contentPanel.Controls.Add(userControl1);
// 根据需要切换显示不同的控件内容
button1.Click += (s, args) =>
{
contentPanel.Controls.Clear();
contentPanel.Controls.Add(userControl1);
};
button2.Click += (s, args) =>
{
contentPanel.Controls.Clear();
contentPanel.Controls.Add(userControl2);
};
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)