C# 仅为 TableLayoutPanel 单元格绘制外边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12566938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw only outer border for TableLayoutPanel Cells
提问by user1417294
I am using the TableLayoutPanel for example if I have 3 rows and 5 columns. I want to draw only the outer border for the entire panel. By default the the panel provides CellBorderStyle which adds all side borders to all the cells available. Is there any way where we can set only outside borders?
例如,如果我有 3 行和 5 列,我正在使用 TableLayoutPanel。我只想绘制整个面板的外边框。默认情况下,面板提供 CellBorderStyle,它将所有侧边框添加到所有可用单元格。有什么办法可以只设置外部边界吗?
I have provided a sample code below.
我在下面提供了一个示例代码。
TableLayoutPanel tblPanel = new TableLayoutPanel;
tblPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
Label lblName;
TextBox txtName;
Button btnAdd;
int colCnt = 0;
for(int rw =0; rw < 3; rw++)
{
lblName = new Label();
lblName.Name = "mylabel" + rw.ToString();
tblPanel.Controls.Add(lblName, colCnt, rw);
colCnt++;
txtName = new TextBox();
txtName.Name = "mytext" + rw.ToString();
tblPanel.Controls.Add(txtName, colCnt, rw);
colCnt++;
btnAdd = new Button();
btnAdd.Name = "mybutton" + rw.ToString();
tblPanel.Controls.Add(btnAdd, colCnt, rw);
colCnt = 0;
}
回答by Fernando Espinosa
I see you are a very new poster. The code of conduct here is that you are really supposed to show what YOU have tried and pinpoint technical issues. No just ask question in that way (specially ones that make you look like you haven't even tried anything).
我看你是一个很新的海报。这里的行为准则是,您确实应该展示您尝试过的内容并查明技术问题。不只是以这种方式提问(特别是那些让你看起来好像你甚至没有尝试过任何东西的问题)。
That said, and trying to help you, you'd be better off painting the cell border yourself. This of something along the following lines, then customize:
也就是说,为了帮助您,您最好自己绘制单元格边框。这符合以下几行,然后自定义:
public TableForm() {
InitializeComponent();
this.tableLayoutPanel.CellPaint += tableLayoutPanel_CellPaint;
}
private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
e.Graphics.DrawLine(Pens.Black, e.CellBounds.Location, new Point(e.CellBounds.Right, e.CellBounds.Top));
}
At design-time:

在设计时:

At runtime:

在运行时:

回答by n3verLieMe
TableLayOutPanel itself does not support a property for border except CellBorderStyle which is not what you want.
TableLayOutPanel 本身不支持除 CellBorderStyle 之外的边框属性,这不是您想要的。
I suggest you to put your TableLayOutPanel into a Panel control and set Dock property of your TableLayOutPanel to Fill.
我建议您将 TableLayOutPanel 放入 Panel 控件中,并将 TableLayOutPanel 的 Dock 属性设置为 Fill。
Then Set BorderStyle of Panel to what you want (FixedSingle or Fixed3D)
然后将面板的边框样式设置为您想要的(FixedSingle 或 Fixed3D)
回答by toquehead
TableLayoutPanel does in fact support the BorderStyle property, which is what you want. For example:
TableLayoutPanel 实际上支持 BorderStyle 属性,这正是您想要的。例如:
tableLayoutPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
It is decorated with:
它装饰有:
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
So Intellisense won't show it to you, but it is documented and it works. I have no insight into why it is non-browsable.
因此 Intellisense 不会向您显示它,但它已记录在案并且可以正常工作。我不知道为什么它不可浏览。
回答by pot
回答by Tomá? Krása
public TestForm()
{
InitializeComponent();
tableLayoutPanel.Paint += tableLayoutPanel_Paint;
}
private void tableLayoutPanel_Paint(object sender, PaintEventArgs e){
e.Graphics.DrawRectangle(new Pen(Color.Blue), e.ClipRectangle);
}

