保证金不适用于TableLayoutPanel

时间:2017-09-08 23:06:49

标签: c# tablelayoutpanel

我正在尝试根据数据库中的表计数在运行时添加按钮。我在代码中添加了顶部和左侧属性,但它们不会随着表单大小而改变。所以我尝试将TableLayoutPanel实现到我的代码中。我可以在TableLayoutPanel上创建我的按钮。但TableLayoutPanel涵盖了整个表单。我想给双方留出一些空间。我试过新的Padding,但它没有用。我创建了面板或工作流面板,并在其中添加了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));

1 个答案:

答案 0 :(得分:0)

保证金不适用于TableLayoutPanel。您只能使用Padding

tableLayoutPanel1.Padding = new Padding(5);