边距在TableLayoutPanel上不起作用

问题描述 投票:0回答:1

我正在尝试根据数据库中的表计数在运行时添加按钮。我在代码中添加了top属性和left属性,但它们不会随着表单大小而改变。所以我尝试对我的代码实现TableLayoutPanel。我可以在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));
c# tablelayoutpanel
1个回答
0
投票

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

tableLayoutPanel1.Padding = new Padding(5);
© www.soinside.com 2019 - 2024. All rights reserved.