VB.NET Making a new font Bold and italic


Im trying to make an ap with 3 checkboxes for bold italic and underline text

so which ever ones are seleced when you click a button a label will then be styled with that font

so if you check bold and italic then the font will be bold and italic style, and if you uncheck them all then the font will be just regular

In vb 6 this would have been easy by just setting the font properties, but as vb.net is crap it doesnt let u do this but forces u to make a new font and the style thing is stupid cos it does this or and thing, which doesnt do a damn thing except makes everything bold.

so does anyone know how to do this in vb.net

u would think this

           Dim MyFont As New System.Drawing.Font(textFamily.Text, mainTextSize, (System.Drawing.FontStyle.Bold And System.Drawing.FontStyle.Italic And System.Drawing.FontStyle.Underline))

would give u a bold, italic and underlined font but oh no its just bold nothing else

i just want to be able to say if boldbox.checked or italicbox.checked or underlinebox.checked then make a font with these styles

hope that all makes some sense, any help would be great!


Who is Participating?
ptakjaConnect With a Mentor Commented:
no, something like this:

Dim FStyle As FontStyle

If isBold.Checked Then
    FStyle = FontStyle.Bold
End If

If isItalic.Checked Then
  FStyle = FStyle OR FontStyle.Italic
End IF

If IsUnder.Checked Then
  FStyle = FStyle OR FontStyle.Underline
End If

new font(myfamily, mysize, FStyle)
Try using OR instead of AND when combining bits in a bitfield.

Dim MyFont As New System.Drawing.Font(textFamily.Text, mainTextSize, (System.Drawing.FontStyle.Bold OR System.Drawing.FontStyle.Italic OR System.Drawing.FontStyle.Underline))
Incidentally, .NET isn't stupid, its object oriented.  VB6 did not fully support object oriented programming.  The learning curve is high, but once you get the hang of it, I think you will see the benefits of programming in a fully supported object oriented language.
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Hi there...

        Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size, FontStyle.Bold)
        Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size, FontStyle.Underline)
        Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size, FontStyle.Italic)

     i took this snippet from some code i have for a combo box.....note the or statements, instead of the and statements  

      Me.cboSource.Font = New System.Drawing.Font("Tahoma", 9.75!, CType((( _
           System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic) _
           Or System.Drawing.FontStyle.Underline), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))

this is for regular...          Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size, FontStyle.Regular)
"Incidentally, .NET isn't stupid, its object oriented."


Bob LearnedCommented:
Yes, stupid is only a matter of opinion and perspective.  *GRIN*

harveyballAuthor Commented:
so how do i make a simple line from 3 checkboxes to determine what style i want

so something like

new font(myfamily, mysize,(isbold.checked or isitalic.checked or isunder.checked))

something like that
harveyballAuthor Commented:
thank you very much (i still dont like vb! :p im a delphi and java programmer by nature, not used to all this microsoft tat :p)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim MyFontStyle As FontStyle
        MyFontStyle = FontStyle.Regular
        If ChkBox_Bold.Checked Then
            MyFontStyle = FontStyle.Bold
        End If
        If ChkBox_Italic.Checked Then
            MyFontStyle = FontStyle.Italic
        End If
        If ChkBox_UnderLine.Checked Then
            MyFontStyle = FontStyle.Underline
        End If
        Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size, MyFontStyle)
End Sub


Your code would only allow ONE font attribute to be set at a time. ;-)
ohhhhhhhhhhh..... damn... I got it....  thanks ptakja
All Courses

From novice to tech pro — start learning today.