仅绘制TableLayoutPanel单元格的外边框

问题描述 投票:7回答:5

我使用TableLayoutPanel例如,如果我有3行和5列。我想只绘制整个面板的外边框。默认情况下,面板提供CellBorderStyle,它将所有边框边框添加到所有可用单元格中。有什么方法可以只设置外边界吗?

我在下面提供了示例代码。

    TableLayoutPanel tblPanel = new TableLayoutPanel;
    tblPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
    Label lblName;
    TextBox txtName;
    Button btnAdd;
    int colCnt = 0;
    for(int rw =0; rw < 3; rw++)
    {
            lblName = new Label();
            lblName.Name = "mylabel" + rw.ToString();
            tblPanel.Controls.Add(lblName, colCnt, rw);
            colCnt++;

            txtName = new TextBox();
            txtName.Name = "mytext" + rw.ToString();
            tblPanel.Controls.Add(txtName, colCnt, rw);
            colCnt++;

            btnAdd = new Button();
            btnAdd.Name = "mybutton" + rw.ToString();
            tblPanel.Controls.Add(btnAdd, colCnt, rw);

            colCnt = 0;
    }
c# .net winforms
5个回答
5
投票

我看到你是一张很新的海报。这里的行为准则是​​你真的应该展示你所尝试的并找出技术问题。不只是以这种方式提出问题(特别是那些让你看起来甚至没有尝试任何东西的问题)。

那就是说,并试图帮助你,你最好自己画细胞边界。这是以下几行中的内容,然后自定义:

    public TableForm() {
        InitializeComponent();
        this.tableLayoutPanel.CellPaint += tableLayoutPanel_CellPaint;
    }

    private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
        e.Graphics.DrawLine(Pens.Black, e.CellBounds.Location, new Point(e.CellBounds.Right, e.CellBounds.Top));
    }

在设计时:

在运行时:


5
投票

TableLayoutPanel实际上支持BorderStyle属性,这是你想要的。例如:

tableLayoutPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

https://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.borderstyle(v=vs.110).aspx

它装饰有:

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]

所以Intellisense不会向你展示它,但它有记录并且有效。我不知道它为什么不可浏览。


2
投票

TableLayOutPanel本身不支持除了CellBorderStyle之外的边框属性,这不是你想要的。

我建议你将TableLayOutPanel放入Panel控件并将TableLayOutPanel的Dock属性设置为Fill。

然后将Panel的BorderStyle设置为你想要的(FixedSingle或Fixed3D)


1
投票

您可以通过将属性CellBorderStyle更改为Single或所需选择来实现。

物业变动:

enter image description here

样品:

enter image description here


0
投票
public TestForm()
    {
        InitializeComponent();
        tableLayoutPanel.Paint += tableLayoutPanel_Paint;
    }

private void tableLayoutPanel_Paint(object sender, PaintEventArgs e){

       e.Graphics.DrawRectangle(new Pen(Color.Blue), e.ClipRectangle);

    }
© www.soinside.com 2019 - 2024. All rights reserved.