Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在TableLayoutPanel的同一行中的控件内设置控件_C#_Winforms_Tablelayoutpanel - Fatal编程技术网

C# 在TableLayoutPanel的同一行中的控件内设置控件

C# 在TableLayoutPanel的同一行中的控件内设置控件,c#,winforms,tablelayoutpanel,C#,Winforms,Tablelayoutpanel,我创建了一个TableLayoutPanel,希望在其中添加两个按钮,一个挨着另一个,因此我尝试: 首先,我将面板创建为: var pnlButtons = new TableLayoutPanel { Name = "pnlButtons", AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,

我创建了一个TableLayoutPanel,希望在其中添加两个按钮,一个挨着另一个,因此我尝试:

首先,我将面板创建为:

  var pnlButtons = new TableLayoutPanel
            {
                Name = "pnlButtons",
                AutoSize = true,
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                Dock = DockStyle.Bottom,
                RowCount = 1,
                TabIndex = 1
            };

            pnlButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            pnlButtons.ColumnStyles.Add(new ColumnStyle());
            pnlButtons.RowStyles.Add(new RowStyle());

            pnlButtons.HandleCreated += new EventHandler(pnlButtons_Created);
            this.Controls.Add(pnlButtons);
然后在事件处理程序中添加按钮:

private void pnlButtons_Created (object sender, EventArgs e)
        {
            var pnl = (TableLayoutPanel)sender;
            var btnSetAmount = new Button
            {
                Text = "Set Amounts",
                Name = "btnSetAmount",
                Anchor = AnchorStyles.Top | AnchorStyles.Right,
                TabIndex = 0,
                UseVisualStyleBackColor = true

            };
            pnl.Controls.Add(btnSetAmount);

            var btnCancel = new Button
            {
                Text = "Cancel",
                Name = "btnCancel",
                Anchor = AnchorStyles.Top | AnchorStyles.Left,
                TabIndex = 1,
                UseVisualStyleBackColor = true
            };
            pnl.Controls.Add(btnCancel);

        }
但当我运行它时,我会看到这样的情况:

它在另一排。如何才能设置在同一行中?问候

更新:在上面的评论之后,现在显示如下:


TableLayoutPanel在控件集合上有一个覆盖,允许您指定列和行:

pnl.Controls.Add(btnSetAmount, 0, 0);
pnl.Controls.Add(btnCancel, 1, 0);

它起作用了。但现在,如果我想获得该面板的100%宽度,并根据面板宽度创建50%和50%的按钮,我该如何实现这一点?@Leon尝试将列设置为50%,然后停靠填充按钮,而不是锚定按钮。