using System;
using System.Windows.Forms;
namespace TableLayoutPanelExample
{
public class Form1 : Form
{
private TableLayoutPanel tableLayoutPanel1;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
public Form1()
{
// Initialize the TableLayoutPanel
tableLayoutPanel1 = new TableLayoutPanel();
tableLayoutPanel1.Dock = DockStyle.Fill;
tableLayoutPanel1.RowCount = 2;
tableLayoutPanel1.ColumnCount = 2;
tableLayoutPanel1.AutoSize = true;
// Initialize buttons
button1 = new Button { Text = "Button 1" };
button2 = new Button { Text = "Button 2" };
button3 = new Button { Text = "Button 3" };
button4 = new Button { Text = "Button 4" };
// Add buttons to the TableLayoutPanel
tableLayoutPanel1.Controls.Add(button1, 0, 0);
tableLayoutPanel1.Controls.Add(button2, 1, 0);
tableLayoutPanel1.Controls.Add(button3, 0, 1);
tableLayoutPanel1.Controls.Add(button4, 1, 1);
// Add the TableLayoutPanel to the form
this.Controls.Add(tableLayoutPanel1);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1()
:在窗体的构造函数中,我们初始化了 TableLayoutPanel
和四个按钮,并将这些按钮添加到 TableLayoutPanel
的不同单元格中。Controls.Add
方法:用于将控件添加到 TableLayoutPanel
中,参数分别为控件对象、列索引和行索引。Main
方法:这是应用程序的入口点,启动窗体并运行应用程序。通过这段代码,你可以创建一个包含 2 行 2 列的表格布局,并在每个单元格中放置一个按钮。
上一篇:c# 数组转list
下一篇:c# 替换字符串
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站