简体   繁体   English

TableLayoutPanel第一列没有边框?

[英]TableLayoutPanel NO Border Around First Column?

I have a class that extends TableLayoutPanel defined/constructed as follows: 我有一个扩展TableLayoutPanel定义/构造的类,如下所示:

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

When my table is drawn it has borders around all 5 columns (as one would expect given the code to set the CellBorderStyle above). 当我的表被绘制时,它具有所有5列的边界(正如人们所期望的那样,给定代码来设置上面的CellBorderStyle )。

Is there a way I can prevent a border from being drawn around the first column? 有没有办法阻止在第一列周围绘制边框?

I know you can add a CellPaint callback: 我知道你可以添加一个CellPaint回调:

this.CellPaint += tableLayoutPanel_CellPaint;

and in this callback you can do things such as change the border color for a particular column: 在此回调中,您可以执行诸如更改特定列的边框颜色之类的操作:

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

But how do you draw "No" rectangle at all? 但是你怎么画“No”矩形呢?

I tried setting the color to Color.Empty but that didn't work: 我尝试将颜色设置为Color.Empty,但这不起作用:

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

Try it the other way around. 反过来尝试一下。 Only draw the borders around the cells you want to have a border: 只绘制要设置边框的单元格周围的边框:

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

Obviously, set your borders back to none so the painting can take over the job: 显然,将边框设置为无,以便绘画可以接管作业:

this.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;

The painting of the cell's borders is performed by TableLayoutPanel in the OnPaintBackground override. 单元格边框的绘制由OnPaintBackground覆盖中的TableLayoutPanel执行。

To modify the way your borders are painted, you need to set no borders (so the base class doesn't paint anything), and then paint all of the other borders in your own override of OnPaintBackground. 要修改边框的绘制方式,您需要设置无边框(因此基类不绘制任何内容),然后在您自己的OnPaintBackground覆盖中绘制所有其他边框。

TableLayoutPanel uses an internal function, ControlPaint.PaintTableCellBorder to perform the border painting. TableLayoutPanel使用内部函数ControlPaint.PaintTableCellBorder来执行边框绘制。 Since you can't use it, you should have a look at the source code (with Reflector or ILSpy) to see how they did it. 由于您无法使用它,您应该查看源代码(使用Reflector或ILSpy)以了解它们是如何做到的。

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

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