2016-10-04 220 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); 
    } 
} 

但你應該問問自己爲什麼情況已經出現,如果用戶不應該也許實際上看到有一列隱藏。