2016-10-04 231 views
0

我有TableLayoutPanel放置在窗体上。 它有3列和2行。 我已经将TableLayoutPanel的CellBorderStyle属性设置为“Single”。 我想动态地隐藏第二列。 实现这个我下面的代码写:TableLayoutPanel单元格边框问题

tableLayoutPanel1.ColumnStyles[0].Width = 0; 

但随后TableLayoutPanel看起来像below.See边框,边框变得厚: enter image description here 谁能解决这个问题?

回答

2

您需要owner.draw的TLP:

隐藏的第三列:enter image description here

这是一个办法:关闭​​和代码此事件

private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) 
{ 
    Rectangle r = e.CellBounds; 
    using (Pen pen = new Pen(Color.DarkGoldenrod)) 
    { 
     // top and left lines 
     e.Graphics.DrawLine(pen, r.X, r.Y, r.X + r.Width, r.Y); 
     e.Graphics.DrawLine(pen, r.X, r.Y, r.X, r.Y + r.Height); 
     // last row? move hor.lines 1 up! 
     int cy = e.Row == tableLayoutPanel1.RowCount - 1 ? -1 : 0; 
     if (cy != 0) e.Graphics.DrawLine(pen, r.X, r.Y + r.Height + cy, 
           r.X + r.Width, r.Y + r.Height + cy); 
     // last column ? move vert. lines 1 left! 
     int cx = e.Column == tableLayoutPanel1.ColumnCount - 1 ? -1 : 0; 
     if (cx != 0) e.Graphics.DrawLine(pen, r.X + r.Width + cx, r.Y, 
           r.X + r.Width + cx, r.Y + r.Height); 
    } 
} 

但你应该问问自己为什么情况已经出现,如果用户不应该也许实际上看到有一列隐藏。