如何在运行时将文本框的文本设置为粗体?
我正在使用 Windows 窗体,并且有一个文本框,如果文本是某个值,我偶尔会希望将其设置为粗体。
如何在运行时更改字体特性?
我看到有一个名为 textbox1.Font.Bold 的属性,但这是一个仅获取属性。
I'm using Windows forms and I have a textbox which I would occassionally like to make the text bold if it is a certain value.
How do I change the font characteristics at run time?
I see that there is a property called textbox1.Font.Bold but this is a Get only property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
字体本身的粗体属性是只读的,但文本框的实际字体属性不是只读的。您可以将文本框的字体更改为粗体,如下所示:
然后再更改回来:
The bold property of the font itself is read only, but the actual font property of the text box is not. You can change the font of the textbox to bold as follows:
And then back again:
根据您的应用程序,您可能希望在文本更改或相关文本框的焦点/取消焦点时使用该字体分配。
下面是它的外观的快速示例(空表单,只有一个文本框。当文本读取“粗体”时,字体变为粗体,不区分大小写):
Depending on your application, you'll probably want to use that Font assignment either on text change or focus/unfocus of the textbox in question.
Here's a quick sample of what it could look like (empty form, with just a textbox. Font turns bold when the text reads 'bold', case-insensitive):
您可以使用
Extension
方法在常规样式和粗体样式之间切换,如下所示:和用法:
You could use
Extension
method to switch between Regular Style and Bold Style as below:And usage:
以下是切换粗体、下划线和斜体的示例。
Here is an example for toggling bold, underline, and italics.