在 TableLayoutPanel 的同一行内的控件内设置控件
Set controls inside a control inside same row of TableLayoutPanel
我创建了一个 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 在 Controls 集合上有一个覆盖,允许您指定列和行:
pnl.Controls.Add(btnSetAmount, 0, 0);
pnl.Controls.Add(btnCancel, 1, 0);
我创建了一个 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 在 Controls 集合上有一个覆盖,允许您指定列和行:
pnl.Controls.Add(btnSetAmount, 0, 0);
pnl.Controls.Add(btnCancel, 1, 0);