在VB.NET中更改字体属性

Bold is "read-only" in VB.NET. This article tells you how to change that.

在VB.NET中,粗体为“只读”。 本文告诉您如何更改它。

In VB6, it was dead easy to change a font to bold. You simply coded something like Label1.FontBold, but in VB.NET, the Bold property of the Font object for a Label is read-only. So how do you change it?

在VB6中,很容易将字体更改为粗体。 您只需编码类似Label1.FontBold之类的东西 ,但是在VB.NET中,Label的Font对象的Bold属性是只读的。 那你怎么改变呢?

使用Windows窗体更改VB.NET中的字体属性 ( Changing Font Properties in VB.NET With Windows Forms )

Here's the basic code pattern for Windows Forms.

这是Windows窗体的基本代码模式。

Private Sub BoldCheckbox_CheckedChanged( _ByVal sender As System.Object, _ByVal e As System.EventArgs) _Handles BoldCheckbox.CheckedChangedIf BoldCheckbox.CheckState = CheckState.Checked ThenTextToBeBold.Font = _New Font(TextToBeBold.Font, FontStyle.Bold)ElseTextToBeBold.Font = _New Font(TextToBeBold.Font, FontStyle.Regular)End IfEnd Sub

私有子项BoldCheckbox_CheckedChanged(_ByVal发件人作为System.Object,_ByVal e作为System.EventArgs)_Handles BoldCheckbox.CheckedChangedIf BoldCheckbox.CheckState = CheckState.Checked ThenTextToBeBold.Font = _New Font(TextToBeBold.Font_,FontStyle.ToBold) (TextToBeBold.Font,FontStyle.Regular)结束IfEnd子

There's a lot more than Label1.FontBold, that's for sure. In .NET, fonts are immutable. That means once they are created they cannot be updated.

可以肯定的是,不只是Label1.FontBold 。 在.NET中,字体是不可变的。 这意味着一旦创建它们便无法更新。

VB.NET gives you more control than you get with VB6 over what your program is doing, but the cost is that you have to write the code to get that control. VB6 will internally drop one GDI font resource and create a new one. With VB.NET, you have to do it yourself.

与程序VB6相比,VB.NET为您提供了更多的控制权,但是代价是您必须编写代码来获得该控件。 VB6将在内部删除一个GDI字体资源并创建一个新的GDI字体资源。 使用VB.NET,您必须自己做。

You can make things a little more global by adding a global declaration at the top of your form:

您可以通过在表单顶部添加全局声明来使事情更具全局性:

Private fBold As New Font("Arial", FontStyle.Bold)Private fNormal As New Font("Arial", FontStyle.Regular)

私有fBold作为新字体(“ Arial”,FontStyle.Bold)私有fNormal作为新字体(“ Arial”,FontStyle.Regular)

Then you can code:

然后,您可以编写代码:

TextToBeBold.Font = fBold

TextToBeBold.Font = fBold

Note that the global declaration now specifies the font family, Arial, rather than simply using the existing font family of one specific control.

注意,全局声明现在指定了字体系列Arial,而不是简单地使用一个特定控件的现有字体系列。

使用WPF ( Using WPF )

What about WPF? WPF is a graphical subsystem you can use with the .NET Framework to build applications where the user interface is based on an XML language called XAML and the code is separate from the design and is based on a .NET language like Visual Basic. In WPF, Microsoft changed the process yet again. Here's the way you do the same thing in WPF.

WPF呢? WPF是一个图形子系统,可以与.NET Framework一起使用来构建应用程序,其中用户界面基于称为XAML的XML语言,并且代码与设计分开,并且基于.NET语言(如Visual Basic)。 在WPF中,Microsoft再次更改了该过程。 这是您在WPF中执行相同操作的方式。

Private Sub BoldCheckbox_Checked( _ByVal sender As System.Object, _ByVal e As System.Windows.RoutedEventArgs) _Handles BoldCheckbox.CheckedIf BoldCheckbox.IsChecked = True ThenTextToBeBold.FontWeight = FontWeights.BoldElseTextToBeBold.FontWeight = FontWeights.NormalEnd IfEnd Sub

私有子项BoldCheckbox_Checked(_ByVal发送者为System.Object,_ByVal e为System.Windows.RoutedEventArgs)_Handles BoldCheckbox.CheckedIf BoldCheckbox.IsChecked = True ThenTextToBeBold.FontWeight = FontWeights.BoldElseTextToBBold.FontWeight =

The changes are:

更改为:

  • The CheckBox event is Checked instead of CheckedChanged

    CheckBox事件被选中,而不是CheckedChanged
  • The CheckBox property is IsChecked instead of CheckState

    CheckBox属性是IsChecked而不是CheckState
  • The property value is a Boolean True/False instead of the Enum CheckState. (Windows Forms offers a True/False Checked property in addition to CheckState, but WPF doesn't have both.)

    该属性值为布尔值True / False,而不是Enum CheckState。 (Windows窗体除了提供CheckState之外,还提供了True / False Checked属性,但WPF却没有两者都提供。)
  • FontWeight is a dependency property of the Label instead of FontStyle being the property of the Font object.

    FontWeight是Label的依赖项属性,而不是FontStyle是Font对象的属性。
  • FontWeights is a NotInheritable class and Bold is a Static value in that class

    FontWeights是一个NotInheritable类,而Bold是该类中的静态值

Whew!! Do you think Microsoft actually tried to make it more confusing?

哇! 您是否认为Microsoft实际上试图使它更加混乱?

翻译自: https://www.thoughtco.com/changing-font-properties-in-vbnet-3424232

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值