Search code examples
vb.netfontslabelfont-style

Applying more than one font style to a label - vb.net


I have a program that will apply bold, italic, and underline, each with a button click, to a label. But I can't have 2 fontstyles, like a word that is bold and italic at the same time. The following code I tried will override the previous applied style with the new one.

Dim con4 As Label
For Each con4 In Me.Controls
   If con4.Font.Bold = False Then
        con4.Font = New Font(con4.Font, FontStyle.Bold)
   Else
        con4.Font = New Font(con4.Font, FontStyle.Regular)
   End If

What I want to do, is change the label to bold when the button is clicked and the label is normal, and revert it to normal if it was already bold.

I used the same code for italic and underline, but instead I used con5 and con6

I soon saw that this does not allow me to make the Label Bold and Italic, for example. Each button click overrides the previous one. So if you please tell me exactly what to do.


Thanks guys.


Solution

  • The FontStyle enumeration has the Flags attribute applied, which means that you can combine values with an Or operator. For instance, to apply both Bold and Italic you use this:

    FontStyle.Bold Or FontStyle.Italic
    

    The code you have is actually toggling a single style and the most correct way to do that is like this:

    myFont = New Font(myFont, myFont.Style Xor FontStyle.Bold)
    

    That will apply Bold if it's not and remove it if it is without affecting any other styles, so you could have two separate buttons or menu items that each toggled one style.

    If you want to be able to optionally apply more than one style, e.g. using CheckBoxes then I'd suggest something like this:

    Dim style = FontStyle.Regular
    
    If useBold Then
        style = style Or FontStyle.Bold
    End If
    
    If useItalic Then
        style = style Or FontStyle.Italic
    End If
    
    myFont = New Font(myFont, style)
    

    EDIT:

    Here's a more complete example I just put together that uses Buttons to toggle styles:

    Private Sub boldButton_Click(sender As Object, e As EventArgs) Handles boldButton.Click
        ToggleStyle(FontStyle.Bold)
    End Sub
    
    Private Sub italicButton_Click(sender As Object, e As EventArgs) Handles italicButton.Click
        ToggleStyle(FontStyle.Italic)
    End Sub
    
    Private Sub underlineButton_Click(sender As Object, e As EventArgs) Handles underlineButton.Click
        ToggleStyle(FontStyle.Underline)
    End Sub
    
    Private Sub ToggleStyle(style As FontStyle)
        Me.Label1.Font = New Font(Me.Label1.Font, Me.Label1.Font.Style Xor style)
    End Sub
    

    Here's a similar example that uses CheckBoxes to select styles:

    Private Sub boldCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles boldCheckBox.CheckedChanged
        SetStyle()
    End Sub
    
    Private Sub italicCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles italicCheckBox.CheckedChanged
        SetStyle()
    End Sub
    
    Private Sub underlineCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles underlineCheckBox.CheckedChanged
        SetStyle()
    End Sub
    
    Private Sub SetStyle()
        Dim style = FontStyle.Regular
    
        If Me.boldCheckBox.Checked Then
            style = style Or FontStyle.Bold
        End If
    
        If Me.italicCheckBox.Checked Then
            style = style Or FontStyle.Italic
        End If
    
        If Me.underlineCheckBox.Checked Then
            style = style Or FontStyle.Underline
        End If
    
        Me.Label1.Font = New Font(Me.Label1.Font, style)
    End Sub