简体   繁体   English

TableLayoutPanel单元格边框问题

[英]TableLayoutPanel cell border issue

I have TableLayoutPanel placed on windows form. 我将TableLayoutPanel放在Windows窗体上。 It has 3 columns and 2 rows. 它具有3列和2行。 I have set CellBorderStyle property of TableLayoutPanel to "Single". 我已经将TableLayoutPanel的CellBorderStyle属性设置为“ Single”。 I want to hide second column dynamically. 我想动态隐藏第二列。 To achieving this I have write following code: 为此,我编写了以下代码:

tableLayoutPanel1.ColumnStyles[0].Width = 0;

But then TableLayoutPanel will look like below.See the border, border becomes thick: 但是随后TableLayoutPanel将如下所示。看到边框,边框变粗了: 在此处输入图片说明 Can anyone resolve this issue? 谁能解决这个问题?

You will need to owner.draw the TLP: 您将需要owner.draw TLP:

Hiding the 3rd column: 隐藏第三栏: 在此处输入图片说明

This is a way: Turn off the CellBorder and code this event 这是一种方法:关闭CellBorder并对此事件进行编码

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

But you should rather ask yourself why the situation has arisen and if the user shouldn't maybe actually see the there is a column hidden.. 但是您应该问自己,为什么会出现这种情况,以及用户是否可能实际上看不到隐藏的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM