Draw only the outer border for the TableLayoutPanel elements

I use 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 panel provides a CellBorderStyle that adds all side borders to all available cells. Is there a way that we can only set external boundaries?

I gave an example 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; } 
+6
source share
3 answers

I see that you are a very new poster. The code of conduct here is that you really should show what you have tried and identify technical problems. No, just ask the question this way (especially the ones that make you look like you didn't even try anything).

However, and trying to help you, you better paint the cell border. This is something in the following lines, then configure:

  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)); } 

When developing-time: At design-time

At runtime: At runtime

+5
source

TableLayoutPanel actually supports the BorderStyle property you want. For instance:

 tableLayoutPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 

https://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.borderstyle(v=vs.110).aspx

It is decorated with:

 [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] 

So Intellisense will not show it to you, but it is documented and works. I do not understand why it is not available for viewing.

+3
source

TableLayOutPanel itself does not support a property for a border other than a CellBorderStyle, which is not what you want.

I suggest you put your TableLayOutPanel in the control panel and set the Dock property of your TableLayOutPanel to populate.

Then set the BorderStyle panel to whatever you want (FixedSingle or Fixed3D)

+2
source

Source: https://habr.com/ru/post/926111/


All Articles