c#窗体分区域
时间: 2023-07-10 22:04:14 浏览: 92
C# 窗体可以使用 Panel 控件来实现分区域布局。Panel 控件是一个容器控件,可以包含其他控件,它可以按照指定的布局方式排列内部的控件。
以下是一个简单的例子,演示了如何将窗体分成两个区域:
1. 在窗体上添加两个 Panel 控件,分别命名为 panelLeft 和 panelRight。
2. 设置 panelLeft 的 Dock 属性为 Left,设置 panelRight 的 Dock 属性为 Fill。
3. 在 panelLeft 中添加需要显示的控件,例如 Label、Button 等。
4. 在 panelRight 中添加需要显示的控件,例如 TextBox、DataGridView 等。
代码如下:
```csharp
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
```
在 Designer.cs 中:
```csharp
private System.Windows.Forms.Panel panelLeft;
private System.Windows.Forms.Panel panelRight;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView dataGridView1;
private void InitializeComponent()
{
this.panelLeft = new System.Windows.Forms.Panel();
this.panelRight = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.panelLeft.SuspendLayout();
this.panelRight.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// panelLeft
//
this.panelLeft.Controls.Add(this.button1);
this.panelLeft.Controls.Add(this.textBox1);
this.panelLeft.Controls.Add(this.label1);
this.panelLeft.Dock = System.Windows.Forms.DockStyle.Left;
this.panelLeft.Location = new System.Drawing.Point(0, 0);
this.panelLeft.Name = "panelLeft";
this.panelLeft.Size = new System.Drawing.Size(200, 450);
this.panelLeft.TabIndex = 0;
//
// panelRight
//
this.panelRight.Controls.Add(this.dataGridView1);
this.panelRight.Dock = System.Windows.Forms.DockStyle.Fill;
this.panelRight.Location = new System.Drawing.Point(200, 0);
this.panelRight.Name = "panelRight";
this.panelRight.Size = new System.Drawing.Size(600, 450);
this.panelRight.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 18);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 17);
this.label1.TabIndex = 0;
this.label1.Text = "Label1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(15, 53);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(165, 22);
this.textBox1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(15, 94);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Button1";
this.button1.UseVisualStyleBackColor = true;
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(22, 18);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(544, 409);
this.dataGridView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.panelRight);
this.Controls.Add(this.panelLeft);
this.Name = "Form1";
this.Text = "Form1";
this.panelLeft.ResumeLayout(false);
this.panelLeft.PerformLayout();
this.panelRight.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
```
阅读全文