Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw only outer border for TableLayoutPanel Cells

Tags:

c#

.net

winforms

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?

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;
    }
like image 859
user1417294 Avatar asked Sep 24 '12 14:09

user1417294


4 Answers

You'd be better off painting the cell border yourself. Something along the following lines, then customize:

public TableForm() {
    InitializeComponent();
    this.tableLayoutPanel.CellPaint += tableLayoutPanel_CellPaint;
}

private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
    var topLeft = e.CellBounds.Location;
    var topRight = new Point(e.CellBounds.Right, e.CellBounds.Top);
    e.Graphics.DrawLine(Pens.Black, topLeft, topRight);
}

At design-time: At design-time

At runtime: At runtime

like image 127
Fernando Espinosa Avatar answered Nov 02 '22 07:11

Fernando Espinosa


TableLayoutPanel does in fact support the BorderStyle property, which is what you want. For example:

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 won't show it to you, but it is documented and it works. I have no insight into why it is non-browsable.

like image 40
toquehead Avatar answered Nov 02 '22 05:11

toquehead


You can achieve by changing the property CellBorderStyle to Single or desired selection.

Property Change :

enter image description here

Sample :

enter image description here

like image 3
pot Avatar answered Nov 02 '22 07:11

pot


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

I suggest you to put your TableLayOutPanel into a Panel control and set Dock property of your TableLayOutPanel to Fill.

Then Set BorderStyle of Panel to what you want (FixedSingle or Fixed3D)

like image 2
Saeed mohammadi Avatar answered Nov 02 '22 05:11

Saeed mohammadi