Results 1 to 7 of 7

Thread: [RESOLVED] How to make a font bold and italic ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Resolved [RESOLVED] How to make a font bold and italic ?

    I'm writing a little word processor app in VS2005 as a bit of an exercise.

    The problem I'm having is with setting font styles for selected text, I'm using a RichTextBox and I have both 'Bold' and 'Italic' buttons on the toolbar, if I select some text and click 'Bold', the selected text alters state i.e if it's already bold it becomes regular and visa versa, the same goes for the 'Italic' button.

    The thing I cant figure out is how to make the text both bold and italic, say I select some text that is already bold 'TEST TEXT ' and I click the italic button, it changes to 'TEST TEXT '. I cant see a FontStyle of .BoldItalic so I'm pretty stumped

    Is there an easy way of doing this ?

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: How to make a font bold and italic ?

    Hi,

    I'm using VS.Net 2003.

    But you can try this:

    RichTextBox.SelectionStart = RichTextBox.Find
    Dim Italic as New font(RichTextBox.Font, Fontstyle.Italic
    RichTextBox.SelectionFont = ItalicFont

    and

    RichTextBox.SelectionFont = RichTextBox.Find
    Dim BoldFont As New Font(RichTextBox.Font, FontStyle.Bold)
    RichTextBox.SelectionFont = BoldFont

    Hope it helps you!

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to make a font bold and italic ?

    The FontStyle enumeration has been declared with the Flags attribute, which means you can create combined values with a bitwise Or:
    VB Code:
    1. FontStyle.Bold Or FontStyle.Italic
    will create a FontStyle that is both Bold and Italic. It may seem strange to use Or when logically you mean And, but this is a bitwise operation. Each individual value is a series of bits that only has a single bit set. The bitwise Or creates a new value that has both the bit for Bold and the bit for Italic set.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: How to make a font bold and italic ?

    Actually, you can set them both with one statement.

    Dim UsedFont as new font ("Arial", 12, FontStyles.Bold Or FontStyles.Italic)

    The Or may mislead you unless you think of it like a Bitwise Or operation on the binary equivalent of those Enumerated constants.

    If you don't know the current status of the font, you can probably use:

    Dim UsedFont as new font ("Arial", 12, RichTextBox.Font Or FontStyles.Italic)

    to for example add italics to the current format.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: How to make a font bold and italic ?

    Arg, now who's looking in the mirror :P

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to make a font bold and italic ?

    Quote Originally Posted by conipto
    Arg, now who's looking in the mirror :P

    Bill
    I'm sure it was a worthwhile glance anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: How to make a font bold and italic ?

    Once again, thanks for all your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width