C#:在运行时从Label更改字体样式(下划线)的正确方法是什么?
到目前为止,据我所知,如果你想在运行时从标签更改字体相关的属性,主要是所有的字体属性,你必须使用,
lblName.Font = new Font(... etc. etc.
Run Code Online (Sandbox Code Playgroud)
是否有上述快捷方式,但只分配一个新的字体样式,如FontStyle.Underline?
要么
我是否必须继续使用"new Font()"方法并将所有字段分配给它以便为我的标签加下划线?
den*_*ips 15
你这样做的方式:
this.Font = new Font(this.Font, FontStyle.Underline);
Run Code Online (Sandbox Code Playgroud)
是正确的.原因是Font是一种密封且不可变的类型(按设计).在.NET 1中引入时,这看起来有点奇怪,但今天强调函数式编程,并发性和不变性,这种风格可以看得更多.也许这是通过这种方式完成的,因为控件继承了容器的字体,跟踪单个字体属性的更改比跟踪批量字体更改要多得多.
怎么样(我正在使用 .Net Framework 2.0)好吧,这适用于 Web 应用程序。在表单应用程序中它是只读的。
lblName.Font.Underline = true;
定义如下:
//
// Summary:
// Gets or sets a value that indicates whether the font is underlined.
//
// Returns:
// true if the font is underlined; otherwise, false. The default value is false.
[DefaultValue(false)]
[NotifyParentProperty(true)]
public bool Underline { get; set; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13558 次 |
| 最近记录: |