在某些事件中,将标签文本更改为粗体 [英] Change Label Text to bold on certain events

查看:68
本文介绍了在某些事件中,将标签文本更改为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

当发生事件时,如何将标签文本更改为粗体?例如:单击按钮时,标签将变为粗体?我已经在Internet上搜索并尝试但没有任何解决方法.我已经导入了此文件:

导入System.Drawing.Font
导入System.Windows.Forms
导入System.Windows.Media.Fonts

这是我添加的内容,但显示为蓝色字符串.这是我的代码"lblBarcode.Font =带有{.FontStyle = Bold}的新字体".

在新字体"处显示类型" System.Windows.Media.Fonts"没有构造函数"

我也尝试了这段代码,但它也行不通:

将newFont变暗为新字体(lblBarcode.Font,FontStyle.Bold)
lblBarcode.Font = newFont

Hi all,

How could I change the label text to bold when events raise eg: On button click, the label will be bold? I have search in Internet and try but nothing work out.I already imports this:

Imports System.Drawing.Font
Imports System.Windows.Forms
Imports System.Windows.Media.Fonts

This is what I''ve add but it show blue string. This is my code "lblBarcode.Font = New Fonts With {.FontStyle = Bold}".

At "New Fonts" is show that "Types ''System.Windows.Media.Fonts'' has no constructor"

I also try this code but it''s also not work:

Dim newFont as New Font(lblBarcode.Font, FontStyle.Bold)
lblBarcode.Font = newFont

推荐答案

我假设您的程序是Winforms应用程序.否则,请停止阅读.
我不会给您解决方案,但会提供一些建议:
I''m assuming your program is a Winforms application. Otherwise, stop reading.
I won''t give you a solution, but some advice:

  1. 在表单中放置一个不需要的标签.
  2. 在属性网格中,更改其字体(字体,颜色,粗细,大小,所有内容).
  3. 假设您的表单名为"Form1",请打开文件"Form1.Designer.vb"
  4. 浏览至相应的行,并阅读其完成方式
  5. 复制,粘贴,编辑...

  1. Put a label you don''t use in your form.
  2. In the properties grid, change its font (typeface, color, weight, size, everything).
  3. Assuming your form is called "Form1", open the file "Form1.Designer.vb"
  4. Browse to the corresponding line, and read how it''s done
  5. Copy, paste, edit...



我知道了:



I got this:

'myLabel
'
Me.myLabel.Font = New System.Drawing.Font("Lucida Handwriting", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(177, Byte))
Me.myLabel.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(192, Byte), Integer))



请注意,设计者使用的是System.Drawing.Font,而不是System.Windows.Media.Fonts.

希望这会有所帮助,
Pablo.



Note that the designer uses System.Drawing.Font, not System.Windows.Media.Fonts.

Hope this helps,
Pablo.


Pablo Aliskevicius 在解决方案1中给出的建议非常好.
我要添加以下内容
创建两个Font对象,一个使用Bold style,另一个使用Regular style,例如
The advice given by Pablo Aliskevicius in Solution 1 is very good.
I want to add the following
Create two Font objects, one with Bold style and other with Regular style like
Dim BoldFont As New Font("Arial", 12, FontStyle.Bold)
Dim RegularFont As New Font("Arial", 12, FontStyle.Regular)
'To make the Font of Label1 as Bold, in the click event of a Button set 
Label1.Font = BoldFont
'And to change the Font to Regular font, set the following in the Click event of 'another button
Label1.Font = RegularFont


这篇关于在某些事件中,将标签文本更改为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆