如何在 WinForms 中保持标签居中?

发布于 2024-10-05 09:43:38 字数 135 浏览 10 评论 0原文

WinForms 中,我使用 Label 来显示不同的消息,例如成功、失败等。

我想将该标签置于中心表单的中心。我想要一种解决方案,无论标签中只有一个单词还是整个句子,它都能保持居中。

In WinForms I am using a Label to display different messages like success, failure, etc.

I'd like to center that label in the center form. I want a solution that will keep it centered whether there's just one word or a whole sentence in the label.

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

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

发布评论

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

评论(8

慈悲佛祖 2024-10-12 09:43:38

LabelAutoSize 属性设置为 False,将 TextAlign 属性设置为 MiddleCenter 和 <将 code>Dock 属性设置为 Fill

Set Label's AutoSize property to False, TextAlign property to MiddleCenter and Dock property to Fill.

策马西风 2024-10-12 09:43:38

您将通过设置属性 Anchor: None 来实现它。

You will achive it with setting property Anchor: None.

陪我终i 2024-10-12 09:43:38

用于以编程方式设置的一些次要附加内容:

Label textLabel = new Label() { 
        AutoSize = false, 
        TextAlign = ContentAlignment.MiddleCenter, 
        Dock = DockStyle.None, 
        Left = 10, 
        Width = myDialog.Width - 10
};            

Dockstyle 和内容对齐可能与您的需求不同。例如,对于 wpf 表单上的简单标签,我使用 DockStyle.None。

Some minor additional content for setting programmatically:

Label textLabel = new Label() { 
        AutoSize = false, 
        TextAlign = ContentAlignment.MiddleCenter, 
        Dock = DockStyle.None, 
        Left = 10, 
        Width = myDialog.Width - 10
};            

Dockstyle and Content alignment may differ from your needs. For example, for a simple label on a wpf form I use DockStyle.None.

瑾兮 2024-10-12 09:43:38

如果您不想将标签停靠在整个可用区域中,只需设置 SizeChanged 事件而不是 TextChanged 即可。当 autosize 属性设置为 True 时,更改每个字母将更改标签的宽度属性及其文本。因此,顺便说一句,您可以使用任何公式来保持标签在表单中居中。

private void lblReport_SizeChanged(object sender, EventArgs e)
{
    lblReport.Left = (this.ClientSize.Width - lblReport.Size.Width) / 2;
}

If you don't want to dock label in whole available area, just set SizeChanged event instead of TextChanged. Changing each letter will change the width property of label as well as its text when autosize property set to True. So, by the way you can use any formula to keep label centered in form.

private void lblReport_SizeChanged(object sender, EventArgs e)
{
    lblReport.Left = (this.ClientSize.Width - lblReport.Size.Width) / 2;
}
橘寄 2024-10-12 09:43:38

接受的答案对我不起作用有两个原因:

  1. 我设置了 BackColor,因此设置 AutoSize = falseDock = Fill 会导致背景无论如何,
  2. 我不能将 AutoSize 设置为 false,因为我的标签文本是动态的

,相反,我只是使用表单的宽度和标签的宽度来计算左侧偏移量:

MyLabel.Left = (this.Width - MyLabel.Width) / 2;

The accepted answer didn't work for me for two reasons:

  1. I had BackColor set so setting AutoSize = false and Dock = Fill causes the background color to fill the whole form
  2. I couldn't have AutoSize set to false anyway because my label text was dynamic

Instead, I simply used the form's width and the width of the label to calculate the left offset:

MyLabel.Left = (this.Width - MyLabel.Width) / 2;
半夏半凉 2024-10-12 09:43:38

我想做类似的事情,但在带有背景图像的表单上,我发现当标签中的文本更改时,使用此方法重新绘制很明显,所以我执行了以下操作:
* 将标签 AutoSize 设置为 true,将 TextAlign 设置为 MiddleCenter

然后,每次文本更改(我的文本更改是使用计时器完成的)时,我都会调用以下方法:

    private Point GetPosition()
    {
        int y = (this.Height / 2) - (label1.Height / 2);
        int x = (this.Width / 2) - (label1.Width / 2);
        return new Point(x, y);
    }

并将标签的 Location 属性设置为此返回值。这确保了当文本更改时标签始终位于表单的中心,并且全屏表单的重新绘制并不明显。

I wanted to do something similar, but on a form with a background image, I found that when the text in the label changed the repaints were obvious with this method, so I did the following:
* Set the label AutoSize to true and TextAlign to MiddleCenter

Then, each time the text changed (mine was done using a timer) I called the following method:

    private Point GetPosition()
    {
        int y = (this.Height / 2) - (label1.Height / 2);
        int x = (this.Width / 2) - (label1.Width / 2);
        return new Point(x, y);
    }

And set the label's Location property to this return value. This ensured that the label was always in the center of the form when the text changed and the repaints for a full-screen form weren't obvious.

很酷又爱笑 2024-10-12 09:43:38

您可以尝试以下代码片段:

private Point CenterOfMenuPanel<T>(T control, int height=0) where T:Control {
    Point center = new Point( 
        MenuPanel.Size.Width / 2 - control.Width * 2,
        height != 0 ? height : MenuPanel.Size.Height / 2 - control.Height / 2);

    return center;
}

It's Indeed Center

在此处输入图像描述

You could try out the following code snippet:

private Point CenterOfMenuPanel<T>(T control, int height=0) where T:Control {
    Point center = new Point( 
        MenuPanel.Size.Width / 2 - control.Width * 2,
        height != 0 ? height : MenuPanel.Size.Height / 2 - control.Height / 2);

    return center;
}

It's Really Center

enter image description here

白馒头 2024-10-12 09:43:38

'这。'是您所在的表格,其中 lblName 作为您想要居中的项目。此外,“offsetInt”将允许您将标签定位在中心的左侧或右侧。 lblName.Location.Y 维护表单上现有的 Y 高度。

lblName.Location = new Point((int)((this.Width - lblName.Width) / 2) +/- offsetInt, lblName.Location.Y);    

'this.' being the form you are on with lblName as the item you wish to centre. Additionally 'offsetInt' will allow you to position the label left or right of the centre. lblName.Location.Y maintains the existing Y height on the form.

lblName.Location = new Point((int)((this.Width - lblName.Width) / 2) +/- offsetInt, lblName.Location.Y);    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文