如何以编程方式在 C# 中为分配给 Control.Text 属性的字符串/文本加下划线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1021422/
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-06 05:42:52  来源:igfitidea点击:

How to underline a string/text assigned Control.Text property in C# programmatically?

c#

提问by Dhana

How to underline (with Bold & italics) a string/text assigned Control.Text property in C# windows application programmatically?

如何以编程方式在 C# windows 应用程序中为分配给 Control.Text 属性的字符串/文本加下划线(使用粗体和斜体)?

采纳答案by ChrisF

You want the Font property.

您需要 Font 属性。

You can't set the underline of the Font along with the other properties - as they're read only - so you'll need to create a new Font object and assign that to the Font property. There are several constructors that take the bold, italic & underline properties.

您不能将 Font 的下划线与其他属性一起设置 - 因为它们是只读的 - 所以您需要创建一个新的 Font 对象并将其分配给 Font 属性。有几个构造函数采用粗体、斜体和下划线属性。

回答by Niki

Just change the Control.Font property. The Font class has constructors for creating bold/italic/underlined Fonts.

只需更改 Control.Font 属性。Font 类具有用于创建粗体/斜体/下划线字体的构造函数。

回答by zebrabox

Say your control was a Label called myLabel

假设您的控件是一个名为 myLabel 的标签

Font myFont = new Font(myLabel.Font,FontStyle.Bold|FontStyle.Italic|FontStyle.Underline);
myLabel.Font = myFont;

回答by Florian Reischl

Control provides property "Font". You can assign this by using the existing Font as prototype and define the desired style information.

控件提供属性“字体”。您可以通过使用现有字体作为原型来分配它并定义所需的样式信息。

This snippet makes all fonts of all top controls bold, underlined and italic:

此代码段使所有顶部控件的所有字体都加粗、加下划线和斜体:

foreach (Control item in this.Controls)
{
   item.Font = 
      new Font
         (
            item.Font, 
            FontStyle.Underline | FontStyle.Bold | FontStyle.Italic
         );
}

Flo

弗洛