Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# tablelayoutpanel

作者:颠沛流离的伤   发布日期:2025-02-20   浏览:113

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());
        }
    }
}

解释说明:

  1. TableLayoutPanel:这是一个布局控件,允许你将其他控件(如按钮、文本框等)放置在一个表格结构中。你可以指定行和列的数量,并将控件添加到特定的单元格中。
  2. 构造函数 Form1():在窗体的构造函数中,我们初始化了 TableLayoutPanel 和四个按钮,并将这些按钮添加到 TableLayoutPanel 的不同单元格中。
  3. Controls.Add 方法:用于将控件添加到 TableLayoutPanel 中,参数分别为控件对象、列索引和行索引。
  4. Main 方法:这是应用程序的入口点,启动窗体并运行应用程序。

通过这段代码,你可以创建一个包含 2 行 2 列的表格布局,并在每个单元格中放置一个按钮。

上一篇:c# 数组转list

下一篇:c# 替换字符串

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站