无法删除 TableLayoutPanel 中控件之间的间距?
Cannot remove spacing between controls in TableLayoutPanel?
我添加到 TableLayoutPanel
的按钮之间有一些间距。我删除了 Button 中的边框,并在 Panel 中将 Margin 和 Padding 设置为 0。但我继续得到那个间距。
tableLayoutPanel.RowCount 设置为 8,Rows
集合我添加了 8 行 Size Type Absolute
。
我错过了什么吗?这是代码:
private void FillSelectLayout()
{
tableLayoutPanelSelect.Controls.Clear();
tableLayoutPanelSelect.RowStyles.Clear();
tableLayoutPanelSelect.RowCount = 8;
for (int i = 0; i < 8; i++)
{
Button buttonSelector = new Button();
buttonSelector.Height = 64;
buttonSelector.Width = 100;
buttonSelector.FlatStyle = FlatStyle.Flat;
buttonSelector.FlatAppearance.BorderSize = 0;
buttonSelector.BackColor = Color.Yellow;
tableLayoutPanelSelect.Controls.Add(buttonSelector, 0, i);
}
}
显示方式如下:
要删除单元格中按钮之间的 space,将它们的 属性 设置为填充就足够了,然后删除按钮的默认边距:
var b = new Button();
b.Dock = DockStyle.Fill;
b.Margin = new Padding(0);
注:
通常最好将您在单元格中托管的控件的 Dock
属性 设置为 Fill
。这样您的控件将遵循您为列和行设置的 TableLayouPanel
大小调整规则。
TableLayoutPanel
使用控件的Margin
属性设置控件在单元格中的位置。所以如果你不想设置 Dock
而你更喜欢手动设置 Size
,那么只设置 Margin
就足够了。
我 .. 在面板中将边距和填充设置为 0。
你为什么不删除 Buttons
中的 Margin
而不是:
buttonSelector.Margin = new Padding(0);
The Margin property defines the space around the control that keeps
other controls a specified distance from the control's borders.
我在 TableLayoutPanel
中使用不同的控件时遇到了同样的问题
你可以做到这一点
- 转到设计视图
- 点击属性
- 转到列,单击列旁边的文本框时,最右侧会出现一个按钮 (...),单击它
- 弹出 window 出现,Select AutoSize(而不是绝对值或百分比)。
- 在
Show:
select Rows
中的同一个 window 并再次 select 自动调整大小。
- 点击确定就完成了。
我添加到 TableLayoutPanel
的按钮之间有一些间距。我删除了 Button 中的边框,并在 Panel 中将 Margin 和 Padding 设置为 0。但我继续得到那个间距。
tableLayoutPanel.RowCount 设置为 8,Rows
集合我添加了 8 行 Size Type Absolute
。
我错过了什么吗?这是代码:
private void FillSelectLayout()
{
tableLayoutPanelSelect.Controls.Clear();
tableLayoutPanelSelect.RowStyles.Clear();
tableLayoutPanelSelect.RowCount = 8;
for (int i = 0; i < 8; i++)
{
Button buttonSelector = new Button();
buttonSelector.Height = 64;
buttonSelector.Width = 100;
buttonSelector.FlatStyle = FlatStyle.Flat;
buttonSelector.FlatAppearance.BorderSize = 0;
buttonSelector.BackColor = Color.Yellow;
tableLayoutPanelSelect.Controls.Add(buttonSelector, 0, i);
}
}
显示方式如下:
要删除单元格中按钮之间的 space,将它们的 属性 设置为填充就足够了,然后删除按钮的默认边距:
var b = new Button();
b.Dock = DockStyle.Fill;
b.Margin = new Padding(0);
注:
通常最好将您在单元格中托管的控件的
Dock
属性 设置为Fill
。这样您的控件将遵循您为列和行设置的TableLayouPanel
大小调整规则。TableLayoutPanel
使用控件的Margin
属性设置控件在单元格中的位置。所以如果你不想设置Dock
而你更喜欢手动设置Size
,那么只设置Margin
就足够了。
我 .. 在面板中将边距和填充设置为 0。
你为什么不删除 Buttons
中的 Margin
而不是:
buttonSelector.Margin = new Padding(0);
The Margin property defines the space around the control that keeps other controls a specified distance from the control's borders.
我在 TableLayoutPanel
你可以做到这一点
- 转到设计视图
- 点击属性
- 转到列,单击列旁边的文本框时,最右侧会出现一个按钮 (...),单击它
- 弹出 window 出现,Select AutoSize(而不是绝对值或百分比)。
- 在
Show:
selectRows
中的同一个 window 并再次 select 自动调整大小。 - 点击确定就完成了。