TableLayoutPanel第一列没有边框?

时间:2012-08-23 22:40:11

标签: c# winforms tablelayoutpanel

我有一个扩展TableLayoutPanel定义/构造的类,如下所示:

public class MyTableLayout : TableLayoutPanel
{
    public MyTableLayout()
    {
        this.ColumnCount = 5;
        this.RowCount = 1;
        this.CellBorderStyle = TableLayoutPanelCellBorderStyle.Outset;
    }
}

当绘制我的表时,它的所有5列都有边框(正如人们所期望的那样,给定代码来设置上面的 CellBorderStyle )。

有没有办法阻止在第一列周围绘制边框?

我知道你可以添加一个CellPaint回调:

this.CellPaint += tableLayoutPanel_CellPaint;

并且在此回调中,您可以执行诸如更改特定列的边框颜色之类的操作:

private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    if (e.Column == 0 && e.Row == 0)
    {
        e.Graphics.DrawRectangle(new Pen(Color.Red), e.CellBounds);
    }
}

但是你怎么画“No”矩形呢?

我尝试将颜色设置为Color.Empty,但这不起作用:

e.Graphics.DrawRectangle(new Pen(Color.Empty), e.CellBounds);

2 个答案:

答案 0 :(得分:1)

反过来尝试一下。只绘制要设置边框的单元格周围的边框:

private void tableLayoutPanel_CellPaint(object sender, 
                                        TableLayoutCellPaintEventArgs e) {
  if (e.Column > 0 && e.Row == 0) {
    e.Graphics.DrawRectangle(new Pen(Color.Red), e.CellBounds);
  }
}

显然,将边框设置为无,以便绘画可以接管作业:

this.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;

答案 1 :(得分:1)

单元格边框的绘制由OnPaintBackground覆盖中的TableLayoutPanel执行。

要修改边框的绘制方式,您需要设置无边框(因此基类不会绘制任何内容),然后在您自己的OnPaintBackground覆盖中绘制所有其他边框。

TableLayoutPanel使用内部函数ControlPaint.PaintTableCellBorder来执行边框绘制。由于你不能使用它,你应该看看源代码(使用Reflector或ILSpy)来看看它们是如何做到的。

相关问题