TableLayoutPanel 标签对齐到左上角?

发布于 2024-09-24 22:59:59 字数 118 浏览 6 评论 0原文

我有一个 TableLayoutPanel,每次我将标签放入其中一个单元格时,它就会捕捉到左上角。我怎样才能做到不这样做或改变它的捕捉位置。

另外,是否可以更改特定单元格的背景颜色?

谢谢!

I have a TableLayoutPanel and every time I put a label into one of the cells, it snaps to the top left corner. How can I get to it not do this or change where it snaps.

Also, is it possible to change a specific cell's background color?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

轻拂→两袖风尘 2024-10-01 22:59:59

Soo,您可以通过设置标签上的 Dock 属性来控制标签在单元格中“捕捉”的位置。它将停靠在细胞的范围内。我不相信你可以改变单元格的背景颜色。解决此问题的一种方法是在每个单元格中放置一个面板,将其设置为使用完整单元格停靠,并设置面板的背景色。

Soo, you can control where a label 'snaps' in a cell by setting the Dock property on the label. It will dock within the confines of the cell. I don't believe you can change the background color of a cell. One way to work around this is to put a panel in each cell, set it to dock using the full cell, and set the back ground color of the panel.

当爱已成负担 2024-10-01 22:59:59

要更改单元格中控件的位置,请使用控件的 Anchor 属性。

要更改 TableLayoutPanel 中单元格的背景颜色,请使用控件的 CellPaint 事件来测试正在绘制的列和/或行并相应地设置颜色。

下面将把 1、1 处的单元格的背景颜色设置为红色:

    private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
        if (e.Column == 1 && e.Row == 1) {
            e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
        }
    }

To change the location of a control in a cell use the control's Anchor property.

To change the background color of a cell within a TableLayoutPanel, use the control's CellPaint event to test which Column and/or Row is being painted and set the color accordingly.

The following will set the background color of the cell at 1, 1 to red:

    private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
        if (e.Column == 1 && e.Row == 1) {
            e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
        }
    }
韵柒 2024-10-01 22:59:59

@Jay Riggs 有答案。稍微解释一下:

我遇到过这个麻烦。我认为 .Dock 不会成功。就我而言,我通过设置成功了

            lbl.Anchor = AnchorStyles.None; // to center the label. causes empty left space.
            // OR
            lbl.Anchor = AnchorStyles.Left // label sticks to left of the container and hence looks neatly aligned along with other controls.

@Jay Riggs has the answer. To slightly explain on it:

I've had this trouble. I dont think .Dock would do the trick. In my case, I have been successful by setting

            lbl.Anchor = AnchorStyles.None; // to center the label. causes empty left space.
            // OR
            lbl.Anchor = AnchorStyles.Left // label sticks to left of the container and hence looks neatly aligned along with other controls.
戏舞 2024-10-01 22:59:59

设置锚点属性是比通常停靠更好的解决方案。您还可以使用边距属性来调整单元格内标签的位置。如果需要标签与同一行中的文本框对齐,将标签的上边距设置为 6 左右会很有用。

我可以明确地说,你不能在一个单元格中放置多个控件。按照其他人的建议进行操作,将标签添加到面板中,然后将面板添加到单元格中。流动布局面板对此很有用,只是不要过度,否则事情会变得非常混乱。

Setting the anchor property is a better solution than dock normally. You can also use the margin property to adjust the position of a label within a cell. It's useful to set the top margin of a lable to 6 or so if you need it to line up with textboxes in the same row.

I can state categorically that you cannot put more than one control in a cell. Do as others have suggested and add your labels to a panel then add the panel to the cell. Flowlayout panels can be good for that, just don't overdo it or things will get very messy.

云之铃。 2024-10-01 22:59:59

首先将标签的 AutoSize 属性设置为 False
然后根据需要设置 TextAlign 属性。它不会再次跨越到左上角。

First set AutoSize property of label to False.
Then set TextAlign property as you desire. It won't span to top left again.

雨巷深深 2024-10-01 22:59:59

通过使用标签的 Dock 和 TextAlign 属性,您可以在 tablelayoutpanel 中所需的位置设置标签文本

By using Dock and TextAlign Property of label you can set label Text on required place in tablelayoutpanel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文