Solved

VB.NET Making a new font Bold and italic

Posted on 2004-08-24
13
9,196 Views
Last Modified: 2013-12-03
Hi

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!

Thanks

Harvey
0
Comment
Question by:harveyball
  • 4
  • 4
  • 2
  • +3
13 Comments
 
LVL 14

Expert Comment

by:ptakja
ID: 11885394
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))
0
 
LVL 14

Expert Comment

by:ptakja
ID: 11885410
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.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11885430
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)

-Baan
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 11885438
     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))

~b
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11885472
this is for regular...          Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size, FontStyle.Regular)
0
 
LVL 8

Expert Comment

by:gregasm
ID: 11885585
"Incidentally, .NET isn't stupid, its object oriented."

BUAHAHAHA

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 11885630
Yes, stupid is only a matter of opinion and perspective.  *GRIN*

Bob
0
 

Author Comment

by:harveyball
ID: 11885773
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
0
 
LVL 14

Accepted Solution

by:
ptakja earned 125 total points
ID: 11885834
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)
0
 

Author Comment

by:harveyball
ID: 11885962
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)
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11886000

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

-Baan
0
 
LVL 14

Expert Comment

by:ptakja
ID: 11886030
Baan,

Your code would only allow ONE font attribute to be set at a time. ;-)
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11886069
ohhhhhhhhhhh..... damn... I got it....  thanks ptakja
-Baan
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now