C# 在设计时将 NewLine 添加到标签的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13295116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:07:45  来源:igfitidea点击:

Add NewLine to label's Text at design time

c#winformslabelnewlinewindows-applications

提问by SilverLight

How can I add newlines to a Label's Textat design time? There are some posts on Stack Overflow on how to do this in code-behind, but there is no post about that for design time yet, it seems?

我如何添加新行到LabelText在设计时?Stack Overflow 上有一些关于如何在代码隐藏中执行此操作的帖子,但似乎还没有关于设计时的帖子?

采纳答案by Nikola Davidovic

When you click on the label Text property in the Property window for the label, a drop down will appear in which you can, when you press Enter, go to the new line. I just tried it, and it works in Visual Studio 2010.

当您在标签的属性窗口中单击标签文本属性时,将出现一个下拉列表,您可以在其中按下Enter,转到新行。我刚试过,它在 Visual Studio 2010 中工作。

Here's a screenshot to clarify:

这是一个屏幕截图以澄清:

Editing multiline label

编辑多行标签

回答by py2020

Design Time \r\n will do the trick -

设计时间 \r\n 会解决问题 -

      label1.Text = "Multi-line \r\nlabel"

Also you can try setting in designer generated code -

您也可以尝试在设计器生成的代码中进行设置 -

        this.label2.Location = new System.Drawing.Point(151, 120);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(35, 13);
        this.label2.TabIndex = 1;
        this.label2.Text = "Multi-line \r\n label";

Run time -

运行 -

      label1.Text = "Multi-line" + Environment.NewLine + "label";

回答by Vaibhav Bhatia

You can use <br />in your string, for example :

您可以<br />在字符串中使用,例如:

MyLabel.Text = "This is my text" + "<br />" + "This is my new line text";

回答by Dave Farmer

When you get the formatting box to drop down, use 'Shift+Enter' to go to a new line. 'Enter' just causes the box to close. At least, that's my experience in VS2015.

当您将格式框下拉时,使用“Shift+Enter”转到新行。'Enter' 只会使框关闭。至少,这是我在 VS2015 中的经验。

回答by Yashwant Kadam

Set Autosize to False using Properties > Layout > Autosize then set Width and Height parameters depending on your text size using Properties > Layout > Size. This worked for me with Width as 60 and Height as 40 for Label to display like below.
3: Auto

使用 Properties > Layout > Autosize 将 Autosize 设置为 False,然后使用 Properties > Layout > Size 根据您的文本大小设置 Width 和 Height 参数。这对我有用,宽度为 60,高度为 40,标签显示如下。
3:自动

H: Home

H:首页