RichTextBox SelectionFont (applying 2 styles?)

skyfe

Member
Joined
Jan 16, 2009
Messages
14
Programming Experience
3-5
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 :eek:
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
You have to combine the new FontStyle with the existing FontStyle in bitwise fashion:
VB.NET:
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
 
Top Bottom