Change Font Style

Hi,

I am a beginner in VB.Net and want to ask a question.
I hope somebidy can help me.

I want to change the font style from a text in textbox by checking a checkbox.
When checkbox Bold checked, the text in textbox change to Bold.
When checkbox Italic checked, the text in textbox change to Italic. Etc, etc.....

I have my code here:
----------------
If tbxTeks.Font.Bold Then
tbxTeks.Font = New System.Drawing.Font(tbxTeks.Font,
tbxTeks.Font.Style Xor FontStyle.Bold)
Else
tbxTeks.Font = New System.Drawing.Font(tbxTeks.Font,
tbxTeks.Font.Style Or FontStyle.Bold)
End If
----------------
--> tbxTeks is a textbox

But I get an error message :
An unhandled exception of type 'System.ArithmeticException' occurred in system.drawing.dll

Additional information: Overflow or underflow in the arithmetic operation.

Can anybidy help me please

Thanks in advance.
Kusmanto


Answer this question

Change Font Style

  • Stalwart team member

    If cbB.checked Then
    tbxTeks.Font = New System.Drawing.Font(tbxTeks.Font,
    tbxTeks.Font.Style or FontStyle.Bold)
    ElseIf cbI.checked Then
    tbxTeks.Font = New System.Drawing.Font(tbxTeks.Font,
    tbxTeks.Font.Style Or FontStyle.Italic)
    End If


  • DGK

    Thanks for your help.
    I will try it out and let you know the result soon.

    Rgds,
    Juliando

  • Mike 2^2

    Did you mean Xor



  • Lucian

    I don't understand what this is doing Surely you want to either add or remove the Bold style Here, you're either adding Bold, or you add Italic. Have you tried getting rid of the or statement altogether to see what happens I would have thought the Xor would have caused your error, by stripping FontStyle.Bold without adding anything ( not sure if 0 is a valid value, but just trying to understand why your code is failing ).



  • alejack-sw

    Joe,

    It didn't work.
    I got the same error message :
    An unhandled exception of type 'System.ArithmeticException' occurred in system.drawing.dll

    Additional information: Overflow or underflow in the arithmetic operation.

    I have also tried to change :
    tbxTeks.Font = New System.Drawing.Font(tbxTeks.Font, style)

    But also didn't work.

    Please help me to find it out.

    Thanks,
    Juliando

  • Jesus Ponce

    Dim style As FontStyle = IIf(tbxTeks.Font.Style And FontStyle.Bold, tbxTeks.Font.Style Xor FontStyle.Bold, tbxTeks.Font.Style Or FontStyle.Bold)

    tbxTeks.Font = New Font(tbxTeks.Font, style)

    Just another way to do it. Use the same for italics.

    -Joe



  • Jantheman

    The error message show at the line where the statement NEW take place.

    Juliando

  • Change Font Style