【问题标题】:Margin doesn't work on TableLayoutPanel边距在 TableLayoutPanel 上不起作用
【发布时间】:2018-02-17 22:01:11
【问题描述】:

我正在尝试根据数据库中的表数在运行时添加按钮。我在代码上使用 top 和 left 属性添加它们,但它们不会随着表单大小而改变。所以我尝试在我的代码中实现 TableLayoutPanel。我可以在 TableLayoutPanel 上创建我的按钮。但是 TableLayoutPanel 覆盖了整个表单。我想给双方一些空间。我尝试了新的填充,但它不起作用。我创建面板或工作流面板并在其中添加我的表格布局面板。并为他们留出余地,但这也没有奏效。

这是我的代码。

private void frmTables_Load(object sender, EventArgs e)
        {
            Panel panel = new Panel();
            this.Controls.Add(panel);
            panel.Dock = DockStyle.Fill;
            panel.Margin = new Padding(20);
            TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();
            panel.Controls.Add(tableLayoutPanel1);
            SqlConnection con = new SqlConnection(gnl.connectionString);
            SqlCommand cmd = new SqlCommand("SELECT [TableId],[IsEmpty] FROM [serin].[dbo].[Table] WHERE[IsActive] = 1", con);
            tableLayoutPanel1.AutoScroll = true;
            tableLayoutPanel1.RowCount = 1;
            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
            tableLayoutPanel1.ColumnCount = 4;
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
            tableLayoutPanel1.RowStyles.Clear();
            tableLayoutPanel1.AutoSize = true;
            tableLayoutPanel1.Margin = new Padding(5);
            tableLayoutPanel1.Dock = DockStyle.Fill;



            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<Button> buttons = new List<Button>();
            //int left = 104;
            //int top = 79;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i % 4 == 0 && i != 0)
                {
                    tableLayoutPanel1.RowCount = tableLayoutPanel1.RowCount + 1;
                    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
                    //left = 104;
                    //top += 120;
                }
                Button newButton = new Button();
                buttons.Add(newButton);
                this.Controls.Add(newButton);
                makeTables(newButton, i, int.Parse(dt.Rows[i][1].ToString()));
                tableLayoutPanel1.Controls.Add(newButton);
                newButton.Dock = DockStyle.Fill;
                //newButton.Left = left;
                //left += 136;
                //newButton.Top = top;

            }
            tableLayoutPanel1.RowCount = tableLayoutPanel1.RowCount + 1;
            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));

【问题讨论】:

  • 不要使用布局面板,它的属性有限。使用面板并创建您自己的网格。看起来您只是在每个面板单元格中放置文本。因此,您可以制作一个大面板,然后将每个单元格制作为另一个面板,或者为每个单元格添加一个文本框。如果您想使用布局面板,然后向每个网格添加一个文本框,这将允许您有边距。文本的边距将是文本框的左侧、顶部、宽度和高度。然后将每个字符串放入文本框而不是面板中。
  • 如果你想要一张桌子,你可以留在 TLP! - 但填充将赢得保证金!移除底座并设置所有四个锚点!

标签: c# tablelayoutpanel


【解决方案1】:

边距不适用于 TableLayoutPanel。您只能使用Padding

tableLayoutPanel1.Padding = new Padding(5);

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 2017-07-11
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    相关资源
    最近更新 更多