Bold is 'read-only' in VB.NET. This article tells you how to change that.
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?
Changing Font Properties in VB.NET With Windows Forms
Here's the basic code pattern for Windows Forms.
Private Sub BoldCheckbox_CheckedChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BoldCheckbox.CheckedChanged
If BoldCheckbox.CheckState = CheckState.Checked Then
TextToBeBold.Font = _
New Font(TextToBeBold.Font, FontStyle.Bold)
Else
TextToBeBold.Font = _
New Font(TextToBeBold.Font, FontStyle.Regular)
End If
End Sub
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BoldCheckbox.CheckedChanged
If BoldCheckbox.CheckState = CheckState.Checked Then
TextToBeBold.Font = _
New Font(TextToBeBold.Font, FontStyle.Bold)
Else
TextToBeBold.Font = _
New Font(TextToBeBold.Font, FontStyle.Regular)
End If
End Sub
The dash style of a line is specified by the StrokeDashArray property that gets or sets a collection of double variables: Style « Windows Presentation Foundation « VB.Net Tutorial VB.Net Tutorial Windows Presentation Foundation. How to Formatting in DataGridView The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. You can extend the DataGridView control in a number of ways to build custom behaviors into your applications.
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.
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.
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)
Private fNormal As New Font('Arial', FontStyle.Regular)
Then you can code:
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.
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.
Private Sub BoldCheckbox_Checked( _
ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs) _
Handles BoldCheckbox.Checked
If BoldCheckbox.IsChecked = True Then
TextToBeBold.FontWeight = FontWeights.Bold
Else
TextToBeBold.FontWeight = FontWeights.Normal
End If
End Sub
ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs) _
Handles BoldCheckbox.Checked
If BoldCheckbox.IsChecked = True Then
TextToBeBold.FontWeight = FontWeights.Bold
Else
TextToBeBold.FontWeight = FontWeights.Normal
End If
End Sub
The changes are:
- The CheckBox event is Checked instead of CheckedChanged
- The CheckBox property is IsChecked instead of 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.)
- FontWeight is a dependency property of the Label instead of FontStyle being the property of the Font object.
- FontWeights is a NotInheritable class and Bold is a Static value in that class
Whew!! Do you think Microsoft actually tried to make it more confusing?
-->Definition
Gets or sets the style used for dashed lines drawn with this Pen.
Property Value
A DashStyle that represents the style used for dashed lines drawn with this Pen.
Exceptions
ArgumentExceptionArgumentExceptionArgumentExceptionArgumentException
The DashStyle property is set on an immutable Pen, such as those returned by the Pens class.
Remarks
A value of Custom for this property specifies that a custom pattern of dashes and spaces, defined by the DashPattern property, makes up lines drawn with this Pen. If the value of this property is Custom and the value of the DashPattern property is
null
, the pen draws solid lines.