Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RichTextBox1.SelectionFont bold

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,529
US

I know how to make font in RichTextBox1 bold:
[tt]
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
[/tt]
How can I detect (check) if the selected text is bold?

I want to have one button that would make font bold (if it is not) or make font regular (if it is bold already)

Have fun.

---- Andy
 
Simple!!!

If RichTextBox1.Font.Bold = True Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Regular)
ElseIf RichTextBox1.Font.Bold = False Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
End If
RichTextBox1.Font = RichTextBox1.SelectionFont
 

Thank you Antzelinaki, but your code will change the entire text in the RichTextBox1 to bold or normal.

But my question was: How can I detect (check) if the selected text is bold?


Have fun.

---- Andy
 
On MSDN, the definition of RichTextBox.SelectionFont property states:

"Gets or sets the font of the current text selection or insertion point."

Use the "Gets" part of the property:

If RichTextBox1.SelectionFont.Bold = True Then
RichTextBox1.SelectionFont.Bold = False
Else
RichTextBox1.SelectionFont.Bold = True
EndIf


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Gets" part works OK, the problem is with the "sets" part:

On both of these lines:[tt]
RichTextBox1.SelectionFont.Bold = False
RichTextBox1.SelectionFont.Bold = True[/tt]

I get an error: Property 'Bold' is 'Read Only' :-(

Have fun.

---- Andy
 
Here's the full MSDN example:

Private Sub ToggleBold()
If richTextBox1.SelectionFont IsNot Nothing Then
Dim currentFont As System.Drawing.Font = richTextBox1.SelectionFont
Dim newFontStyle As System.Drawing.FontStyle

If richTextBox1.SelectionFont.Bold = True Then
newFontStyle = FontStyle.Regular
Else
newFontStyle = FontStyle.Bold
End If

richTextBox1.SelectionFont = New Font( _
currentFont.FontFamily, _
currentFont.Size, _
newFontStyle _
)
End If
End sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Andrzerek, it's just a small change in my code. ;)
Here you are!!! Have fun :D

If RichTextBox1.SelectionFont.Bold = True Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Regular)
ElseIf RichTextBox1.SelectionFont.Bold = False Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
End If
RichTextBox1.SelectionFont = RichTextBox1.SelectionFont
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top