Font Issues

JDYoder

Well-known member
Joined
Nov 18, 2003
Messages
144
Some days, how I yearn for the good old days of VB6.

All I want to do is set some Font properties on a label. Lo and behold, true to form, VB.Net makes what should be easy to be difficult since Bold, Italic, and Underline are read-only. So I dig and find that I can set a label to Bold like this...

Label1.Font = New Font(Label1.Font, FontStyle.Bold)

No big deal, I think. Now I want to set the labels font to Italic as well. So I run this...

Label1.Font = New Font(Label1.Font, FontStyle.Italic)

Which does indeed set the font to be Italic. However, it also removed my previously set Bold. Same with Underline. Thats issue #1.

Issue #2 is that I would also like to be able to toggle Bold off. As far as I can tell, the only way to do that is to make the font to be Regular like this...

Label1.Font = New Font(Label1.Font, FontStyle.Regular)

However, once again, that will also remove any italics, underlines, etc. So whats the solution?

And lastly, theres Issue #3, where I also need to be able to change the font size of a label. Any help on these items would be greatly appreciated. Not only will you have my undying appreciation, but I just might name my firstborn after you.
 
I havent tried this but maybe imbedding the font changes something like this?


lblTest.Font=New System.Drawing.Font("Arial",10,(System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic),System.Drawing.GrahicsUnit.Point(CType(0,Byte))


I would guess font size is tricky because the lable object needs to be sized to except a larger font.
 
I just tried this and it worked great.

lblTest.Font = New Font("Arial",10,(FontStyle.Bold Or FontStyle.Italic))
 
But that assumes you know at that moment what other font values are to be assigned to the control, which I dont. Otherwise youd need to get them first from the control, with something like this...

Dim iItalic As Integer = IIf(Label1.Font.Italic, FontStyle.Italic, 0)
Dim iUnderline As Integer = IIf(Label1.Font.Underline, FontStyle.Underline, 0)
Dim iStrikeout As Integer = IIf(Label1.Font.Strikeout, FontStyle.Strikeout, 0)
Label1.Font = New Font(Label1.Font, FontStyle.Bold Or iItalic Or iUnderline Or iStrikeout)

Very, very ugly. Theres got to be a way to just change one element of the Font without tracking all the others.

As far as Font Size, dont know how tricky it will be, but with the "AutoSize" set to True, Im assuming / hoping that the control will grow as needed, just as it does when I assign the Font Size during design time.
 
Last edited by a moderator:
I agree, it does seem messy, but you could write a simple class that would accept one property and return a font object. It might make your application cleaner.

Sam
 
Not only is it messy, but its downright stupid. Theres no reason it should be so cumbersome to assign a single value to an object. OOP is supposed to make coding easier, not harder. And I still dont know how to assign the font size.

As far as the above, theres got to be an easier, cleaner way.
 
you assign the font size directly after the font name ( before the style ) , its very straight forwards really. eg :
Code:
        Dim fStyle As FontStyle = FontStyle.Bold Or FontStyle.Italic Or FontStyle.Underline

        Label1.Font = New Font("Tahoma", 10, fStyle)
 
This has nothing to do with OOP, but rather with font creation done by the OS.
Here is how you could handle the font change.
Declare a FontStyle object for yourself:
Code:
Dim fstyle As FontStyle
Assign the current style to it:
Code:
fstyle = Me.Font.Style
Then take them away from the combination:
Code:
fstyle -= FontStyle.Bold Or FontStyle.Italic
Or add some:
Code:
fstyle += FontStyle.Bold Or FontStyle.Italic
Then create a font using the resulting value.
 
Aha! I knew there had to be an easier way. Stills seems a bit more difficult than it needs to be, but at least this makes sense. Thanks for the info. With it, Im doing this...

Dim fStyle As FontStyle = Label1.Font.Style
fStyle = fStyle + IIf(chkFontItalic.Checked, FontStyle.Italic, -FontStyle.Italic)
Label1.Font = New Font(Label1.Font, fStyle)


The last line seems a bit odd to me, in that the New Font requires an existing font for the first param. Seems like I should only have to pass the font style, since font style started out equal to Label1s font, which I then tweaked. So to me, it seems fStyle holds all the info it should need.
 
You are creating a new font, so simply passing in the FontStyle value wont do it. You dont need pass in an existing font for the first argument, you can use other overloads. But in your programs case, doing it like that will help if you only wish to modify the font style.
 
Back
Top