我创建了一个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);