How to change Font style ?

jkusmanto

New member
Joined
Feb 28, 2006
Messages
3
Programming Experience
Beginner
Hi all,

I am a beginner in VB.Net. Allow me to ask a question about Font Style.
I hope that you guys -as a expert in VB.Net- can help me.

Here is my problem:
I want to change a font style in a textbox by clicking (check and uncheck) a checkbox.
My code is like this :
Private Sub cbxBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxBold.CheckedChanged
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
End Sub


But I got an 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 with this way, but I got a same error.
If cbxBold.Checked Then
tbxTeks.Font = New System.Drawing.Font(tbxTeks.Font, FontStyle.Bold)
End If

The error occurs at the line where NEW takes place.

Can anybody help me please !

Thanks in advance,
Juliando
 
I dont know exactly what you want to do but why not just change the property of the font as it is? Instead of initializing a whole new Font object just change the property itself.

Private Sub cbxBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxBold.CheckedChanged
If tbxTeks.Font.Bold Then
tbxTeks.Font .Bold = False
Else
tbxTeks.Font .Bold = True
End If
End Sub

Rather do things simply than make them complicated. I hope I answered your question
 
Thanks for your reply, UncleRonin.

Ofcouse we have to make thing as simple as possible.
But your answer doesn't work.
Before I got to my code, I have tried a code like you defined.
When you type in :
tbxTeks.Font.Bold=False
You get an error immediately (This statement is underlined).

Rgds,
Juliando


UncleRonin said:
I dont know exactly what you want to do but why not just change the property of the font as it is? Instead of initializing a whole new Font object just change the property itself.

Private Sub cbxBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxBold.CheckedChanged
If tbxTeks.Font.Bold Then
tbxTeks.Font .Bold = False
Else
tbxTeks.Font .Bold = True
End If
End Sub

Rather do things simply than make them complicated. I hope I answered your question
 
*scratch* I just had a look for myself... it seems that the Font property for textboxes and labels, etc. are all ReadOnly... *sigh* I'd like to help but right now I cant really come up with anything! Plus I'm busy trying to finish a program I'm busy with... when I'm done I'll try and post a solution if one isnt found...
 
Okay, for the program I'm busy on I had to change the Font.Bold property of DataGridViewCells and found the same problem you had! Can you beleive it! Luckily, I've managed to find a way to do it - it's actually really simple! (For Textboxes, etc. that is - DGVCells are 'harder', you have to make sure they have an object reference first).

This is the code I used for the DGVCells in case anybody is interested:

Dim SelectedCell As System.Windows.Forms.DataGridViewCell
For Each SelectedCell In Me.DGVResults.SelectedCells
If IsNothing(SelectedCell.Style.Font) Then
SelectedCell.Style.Font = New System.Drawing.Font(Me.DGVResults.DefaultCellStyle.Font, FontStyle.Bold)
Else
If SelectedCell.Style.Font.Bold Then
SelectedCell.Style.Font = New System.Drawing.Font(Me.DGVResults.DefaultCellStyle.Font, FontStyle.Regular)
Else
SelectedCell.Style.Font = New System.Drawing.Font(Me.DGVResults.DefaultCellStyle.Font, FontStyle.Bold)
End If
End If
Next


For your purposes it's just:

If textbox.Font.Bold Then
textbox.Font = New System.Drawing.Font(textbox.Font, FontStyle.Regular)
Else
textbox.Font = New System.Drawing.Font(textbox.Font, FontStyle.Bold)
End If


I know you tried something identical before but I know for a fact that this works... that you had an arithmetic error before is because of something else.... if it doesn't work for you then I don't know what will!
 
The suggested method is not quite optimal. Here are some methods that will set, clear and toggle a font style. The main thing to note here is that changing one style won't affect any others, e.g. if the font is already bold and you want to italicise it, this code will do so without clearing the bold style.
''' <summary>
''' Sets a style on a <see cref="Font"/>.
''' </summary>
''' <param name="font">
''' The original <b>Font</b>.
''' </param>
''' <param name="style">
''' The style to set.
''' </param>
''' <returns>
''' A new <b>Font</b> identical to <i>font</i> with the specified style set.
''' </returns>
Private Function SetFontStyle(font As Font, style As FontStyle) As Font
    Return New Font(font, font.Style Or style)
End Function

''' <summary>
''' Clears a style on a <see cref="Font"/>.
''' </summary>
''' <param name="font">
''' The original <b>Font</b>.
''' </param>
''' <param name="style">
''' The style to clear.
''' </param>
''' <returns>
''' A new <b>Font</b> identical to <i>font</i> with the specified style cleared.
''' </returns>
Private Function ClearFontStyle(font As Font, style As FontStyle) As Font
    Return New Font(font, font.Style And Not style)
End Function

''' <summary>
''' Toggles a style on a <see cref="Font"/>.
''' </summary>
''' <param name="font">
''' The original <b>Font</b>.
''' </param>
''' <param name="style">
''' The style to toggle.
''' </param>
''' <returns>
''' A new <b>Font</b> identical to <i>font</i> with the specified style toggled.
''' </returns>
''' <remarks>
''' If <i>style</i> was set on <i>font</i> then it will not be set on the new <b>Font</b>
''' and if it was not set on the original then it will be set on the result.
''' </remarks>
Private Function ToggleFontStyle(font As Font, style As FontStyle) As Font
    Return New Font(font, font.Style Xor style)
End Function
Sample usage:
Private Sub BoldToolStripButton_CheckedChanged(sender As Object, e As EventArgs) Handles BoldToolStripButton.CheckedChanged
    TextBox1.Font = If(BoldToolStripButton.Checked,
                       SetFontStyle(TextBox1.Font, FontStyle.Bold),
                       ClearFontStyle(TextBox1.Font, FontStyle.Bold))
End Sub
 
Back
Top