user1417294
user1417294

Reputation: 314

Draw only outer border for TableLayoutPanel Cells

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

Upvotes: 12

Views: 34020

Answers (5)

Fernando Espinosa
Fernando Espinosa

Reputation: 4825

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

Upvotes: 9

public TestForm()
    {
        InitializeComponent();
        tableLayoutPanel.Paint += tableLayoutPanel_Paint;
    }

private void tableLayoutPanel_Paint(object sender, PaintEventArgs e){

       e.Graphics.DrawRectangle(new Pen(Color.Blue), e.ClipRectangle);

    }

Upvotes: 0

pot
pot

Reputation: 261

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

Property Change :

enter image description here

Sample :

enter image description here

Upvotes: 3

toquehead
toquehead

Reputation: 109

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.

Upvotes: 10

Saeed mohammadi
Saeed mohammadi

Reputation: 319

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)

Upvotes: 2

Related Questions