Home > Net >  How to change the fontstyle without creating a new font?
How to change the fontstyle without creating a new font?

Time:01-01

I'm trying to do something that's probably incredibly simple, yet everything I'm trying doesn't seem to work.

I'm making a windows forms app that just lets you preview some custom text using a hand full of different fonts.

What I'm trying to do is change the fontstyle of the label to italics if and when the checkbox is checked. While retaining the current font that it is.

"Display" is the name of my label. "Italicscb" is the name of the check box.

Below is the code I'm currently using:

Private Sub Italicscb_CheckedChanged(sender As Object, e As EventArgs) Handles 
Italicscb.CheckedChanged

    If Italicscb.CheckState = CheckState.Checked Then

        Display.Font = New Font("Arial", 60, FontStyle.Italic)
    Else
        If Italicscb.CheckState = CheckState.Unchecked Then

            Display.Font = New Font("Arial", 60, FontStyle.Bold)
        End If
    End If
End Sub

This works fine, but it requires me to enter a new font name. Which I don't want. I've tried to assign the current font a variable and plug that in, but that gives me an error.

    Dim CF As Font
    CF = Display.Font


    If Italicscb.CheckState = CheckState.Checked Then

        Display.Font = New Font(CF, 60, FontStyle.Italic)
    Else
        If Italicscb.CheckState = CheckState.Unchecked Then

            Display.Font = New Font(CF, 60, FontStyle.Bold)
        End If
    End If
End Sub

I've also tried putting an if then loop in the individual font option buttons. No errors, but nothing happens when I check the box.

  if Italicscb.CheckState = CheckState.Checked Then
        Display.Font = New Font("Freehand521 BT", 60, FontStyle.Italic)
    Else
        Display.Font = New Font("Freehand521 BT", 60, FontStyle.Bold)
    End If

If anyone could point out what I'm doing wrong. I would really appreciate it. I'm making this for my work by the way.

Thank you.

Example

  • Related