Press "Enter" to skip to content

How to Bold, Italic, Underline and Strikeout a Text in Visual Basic

 Tip Abstraction:

        Let us see how applying a text style property of Bold, Italic, Underline and Strikeout using Visual Basic programming codes



Making a text style as Bold, Italic, Underline and strike out are the basic styling property of a text. If you have any experience with any text editor, then you may be more familiar with these properties.
Here we are going to change the style property of a Visual basic Label text into Bold, Italic, Strikeout or Underline and also simultaneously.
Applying only one property is comparatively simple according to applying one more properties together.

Here a label named ‘Label5’ is added to a window form and four checkboxes were added with text and name as ‘Bold’, ‘Italic’, ‘Underline’, and ‘Strikeout’.
Here we have to write our code on checkbox checked changed event. To access this event, just double click on a checkbox.

Here checkbox of ‘Bold’ is double clicked, and write down codes like below:

Private Sub Bold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bold.CheckedChanged

If bold.Checked = True Then
Label5.Font = New Font(Label5.Font, Label5.Font.Style Or FontStyle.Bold)
Else
Label5.Font = New Font(Label5.Font, Label5.Font.Style And Not FontStyle.Bold)

End If
End Sub


Like this code for ‘Italic’ should be like this:

Private Sub Italic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles italic.CheckedChanged

If italic.Checked = True Then
Label5.Font = New Font(Label5.Font, Label5.Font.Style Or FontStyle.Italic)
Else
Label5.Font = New Font(Label5.Font, Label5.Font.Style And Not FontStyle.Italic)

End If
End Sub

Code for ‘Underline’ should be like below:

Private Sub Underline_CheckedChanged (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles underline.CheckedChanged

If underline.Checked = True Then
Label5.Font = New Font(Label5.Font, Label5.Font.Style Or FontStyle.Underline)
Else
Label5.Font = New Font(Label5.Font, Label5.Font.Style And Not FontStyle.Underline)

End If
End Sub

And the codes for ‘Strikeout  are:

Private Sub Strikeout_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strikeout.CheckedChanged

If strikeout.Checked = True Then
Label5.Font = New Font(Label5.Font, Label5.Font.Style Or FontStyle.Strikeout)
Else
Label5.Font = New Font(Label5.Font, Label5.Font.Style And Not FontStyle.Strikeout)

End If
End Sub

Code Explanation:

Here we take the codes for Bold property:

   If bold.Checked = True Then

The codes will work if and only while the checkbox ‘Bold’ is checked


  Label5.Font = New Font

Label5.Font = New Font means the Lable5.Font is assinged a set of properties of New Font.

  Label5.Font,

Label5.Font is accecced.


Label5.Font.Style Or FontStyle.Bold

This is the main code. Label5.Font.Style keeps the current Font Styles  Or FontStyle.Bold add a new style property of Bold

Else
Below codes  will work if and only while the checkbox ‘Bold’ is unchecked

Label5.Font = New Font(Label5.Font, Label5.Font.Style And Not FontStyle.Bold)

Same explanation of ‘if ’ case, except  Label5.Font.Style And Not FontStyle.Bold  means, current label font style minus(remove) Bold property.

Example:

If label has a current property of Italic, then above codes keep the existing property of Italic and a new prerty of ‘bold’ is added while the checkbox is checked)

Like this, the code explanation of Italic, Underline and Strikeout are same.

Sample Output:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: