Results 1 to 3 of 3

Thread: RichTextBox SelectionFont (applying 2 styles?)

  1. #1
    skyfe is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jan 2009
    Posts
    14
    Reputation
    71

    RichTextBox SelectionFont (applying 2 styles?)

    Hi,

    How do I apply 2 font styles to the text selected in a richtextbox?
    Used this method to change fontstyle:

    RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Bold)
    but with this method I can only apply 1 font-style (for example bold OR italic OR underlined, etc.) right? But I want to apply for example 2 front-styles, or just add the font style ( so I can add several front-styles to the selected text, for example the text is bold, and then ALSO make it underlined (with this method I can only change the font-style, only one font-style)), how do I do this?

    Thanks in advanced,

    Skyfe.

    EDIT: fixed, sorry just had to do

    RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Bold + FontStyle.Italic)
    for example
    Last edited by skyfe; 01-18-2009 at 8:57 AM.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,908
    Reputation
    1624
    You have to combine the new FontStyle with the existing FontStyle in bitwise fashion:
    Code:
    Private Sub ApplyBoldStyle()
        Me.RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, _
                                                 Me.RichTextBox1.SelectionFont.Style Or FontStyle.Bold)
    End Sub
    
    Private Sub RemoveBoldStyle()
        Me.RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, _
                                                 Me.RichTextBox1.SelectionFont.Style And Not FontStyle.Bold)
    End Sub
    
    Private Sub ToggleBoldStyle()
        Me.RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, _
                                                 Me.RichTextBox1.SelectionFont.Style Xor FontStyle.Bold)
    End Sub

  3. #3
    kef
    kef is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Aug 2009
    Posts
    1
    Reputation
    0

    thanks

    so simple but useful

    thanks

    Kef

Bookmarks

Posting Permissions

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