winforms - 动态地将列和行添加到 tablelayoutpanel 行中

标签 winforms c#-4.0

enter image description here

嗨,我有一个用 C# 编写的 Windows 窗体应用程序。我使用有 5 行和一列的 tablelayoutpanel。我的问题是我们如何在运行时/动态地将红色列和行(它们在图片中以红色显示)添加到第 3 行?我们以后如何才能向其中添加控件(标签、文本框、按钮......)? 感谢您的建议..

最佳答案

这里是一个例子:

private void GenerateControls()
{
    TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();
    Button button1 = new Button();
    Button button2 = new Button();
    PictureBox pictureBox1 = new PictureBox();
    TextBox textBox1 = new TextBox();
    tableLayoutPanel1.SuspendLayout();


    // tableLayoutPanel1
    tableLayoutPanel1.ColumnCount = 2;
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
    tableLayoutPanel1.Controls.Add(button2, 1, 0);
    tableLayoutPanel1.Controls.Add(button1, 0, 0);
    tableLayoutPanel1.Controls.Add(pictureBox1, 0, 1);
    tableLayoutPanel1.Controls.Add(textBox1, 1, 1);
    tableLayoutPanel1.Location = new System.Drawing.Point(12, 12);
    tableLayoutPanel1.Name = "tableLayoutPanel1";
    tableLayoutPanel1.RowCount = 2;
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 20));
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 30F));
    tableLayoutPanel1.Size = new System.Drawing.Size(388, 301);
    tableLayoutPanel1.TabIndex = 0;
    tableLayoutPanel1.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPanel1_CellPaint);

    // button1
    button1.Dock = DockStyle.Fill;
    button1.Location = new System.Drawing.Point(3, 3);
    button1.Name = "button1";
    button1.Size = new System.Drawing.Size(188, 144);
    button1.TabIndex = 0;
    button1.Text = "button1";
    button1.UseVisualStyleBackColor = true;

    // button2
    button2.Dock = DockStyle.Fill;
    button2.Location = new System.Drawing.Point(197, 3);
    button2.Name = "button2";
    button2.Size = new System.Drawing.Size(188, 144);
    button2.TabIndex = 1;
    button2.Text = "button2";
    button2.UseVisualStyleBackColor = true;

    // pictureBox1
    pictureBox1.Dock = DockStyle.Fill;
    pictureBox1.Location = new System.Drawing.Point(3, 153);
    pictureBox1.Name = "pictureBox1";
    pictureBox1.Size = new System.Drawing.Size(188, 145);
    pictureBox1.TabIndex = 2;
    pictureBox1.TabStop = false;
    //pictureBox1.Image = Image.FromFile(@"C:\somepic.jpg");

    // textBox1
    textBox1.Dock = DockStyle.Fill;
    textBox1.Location = new System.Drawing.Point(197, 153);
    textBox1.Multiline = true;
    textBox1.Name = "textBox1";
    textBox1.Size = new System.Drawing.Size(188, 145);
    textBox1.TabIndex = 3;

    Controls.Add(tableLayoutPanel1);
    tableLayoutPanel1.ResumeLayout(false);
    tableLayoutPanel1.PerformLayout();
}

这个空白将操纵边界

void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    if (e.Column == 0)
    {
        var rectangle = e.CellBounds;
        rectangle.Inflate(-1, -1);

        ControlPaint.DrawBorder3D(e.Graphics, rectangle, Border3DStyle.Raised, Border3DSide.All); // 3D border
    }
    else if (e.Column == 1 && e.Row == 0)
    {
        var rectangle = e.CellBounds;
        rectangle.Inflate(-1, -1);

        ControlPaint.DrawBorder(e.Graphics, rectangle, Color.Red, ButtonBorderStyle.Dotted); // dotted border
    }
}

关于winforms - 动态地将列和行添加到 tablelayoutpanel 行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9516918/

相关文章:

c# - ListView 中选定索引的问题

c#-4.0 - 使用 NHibernate 创建数据库类库

接口(interface)上的 C# 泛型隐式强制转换失败

c# - 如何自动结束 DataGridView 单元格编辑模式?

c# - 在 Windows 8 Pro 平板电脑中使用 WinAPI 抛出 AccessViolationException

c# - 使用换行符将 xml 字符串读入文本框

c# - 从文件夹加载 DLL,C# windows 窗体项目

c# - XmlException - 来自第 3 方的非法 XML;必须处理

linux - 如何在 linux 中运行 c#4.0(visual studio 2010) windows 桌面应用程序?

css - ASP Repeater - 无法在 DIV 中添加类