Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Rich Text Box with which I would like to set multiple styles to. Is this the only way?

VB
frmMain.rtbStuff.Font = New Font(Me.Font, FontStyle.Bold + FontStyle.Italic)


For instance, if I would like to create a function that will allow a developer to customize the styles as so.

VB
Private Function ConfigureFont(Optional ByRef isUnderline As Boolean = False, Optional ByRef isBold As Boolean = False, Optional ByRef isItalized as Boolean = False) As Boolean

If isUnderline Then
Me.rtbStuff.Font.Styles = FontStyle.Underline ' Does NOT work. Read Only 
End If

Me.rtbStuff.Font.Bold = isBold ' Does NOT work. Read Only

Me.rtbStuff.Font.Italic = isItalized ' Does NOT work. Read Only.

End Function


This does not help because it only sets one Font Style. http://msdn.microsoft.com/en-us/library/yh8963yx.aspx[^]

Any suggestions?
Posted

Have a look at the SelectionFont property[^] - it sets a specific font and style to a specific part of the text. Which sound like what you want.
 
Share this answer
 
Comments
Clark Kent123 25-Jan-12 11:07am    
Thank you! It works like I expect it to. :)
OriginalGriff 25-Jan-12 11:54am    
You're welcome!
You would need to set the SelectionFont. It applies to the selected text or from the current caret position. Check the link for more info:
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont.aspx#Y57[^]

Good luck!
 
Share this answer
 
Comments
Clark Kent123 25-Jan-12 11:07am    
Thank you! It works like I expect it to. :)
I think this is what you are looking for. This is a function I'm using in one of my programs. It turns the font styles on/off depending on the current state of the toolstrip buttons.


Private Sub Set_Font_Style()

Dim FStyle As FontStyle

'If Bold Style is not selected then...
If Not tsbtnBold.Checked Then

'Turn off Bold Style
FStyle = rtbItemDetails.SelectionFont.Style And Not FontStyle.Bold

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

Else 'If user selected Bold Style then...

'Turn on Bold Style
FStyle = rtbItemDetails.SelectionFont.Style Or FontStyle.Bold

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

End If

'If Itallic Style is not selected then...
If Not tsbtnItallics.Checked Then

'Turn off Itallic Style
FStyle = rtbItemDetails.SelectionFont.Style And Not FontStyle.Italic

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

Else 'If user selected Bold Style then...

'Turn on Itallic Style
FStyle = rtbItemDetails.SelectionFont.Style Or FontStyle.Italic

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

End If

'If Underline Style is not selected then...
If Not tsbtnUnderline.Checked Then

'Turn off Underline Style
FStyle = rtbItemDetails.SelectionFont.Style And Not FontStyle.Underline

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

Else 'If user selected Underline Style then...

'Turn on Underline Style
FStyle = rtbItemDetails.SelectionFont.Style Or FontStyle.Underline

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

End If

'If Strikeout Style is not selected then...
If Not tsbtnStrkethrough.Checked Then

'Turn off Strikethrough Style
FStyle = rtbItemDetails.SelectionFont.Style And Not FontStyle.Strikeout

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

Else 'If user selected Strikeout Style then...

'Turn on Strikeout Style
FStyle = rtbItemDetails.SelectionFont.Style Or FontStyle.Strikeout

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

End If

'Update Button Status
Button_Control()

'Give RichTextBox Focus.
rtbItemDetails.Focus()

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900