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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing multiple fonts to bold

Status
Not open for further replies.

kulfanumber

Programmer
Jun 24, 2009
21
0
0
PK
I hav used the following code for changing text to bold in an rtb editor. When i select text with multiple fonts it is not working. What maybe the problem?

Dim doc As frmDocument = Me.ActiveMdiChild
On Error Resume Next
If doc.Text1.Focused Then
If doc.Text1.SelectionLength > 0 Then
doc.Text1.SelectionFont = New System.Drawing.Font(doc.Text1.SelectionFont, doc.Text1.SelectionFont.Style Xor FontStyle.Italic)
End If
End IF
 
Are you sure that the all the fonts that you are selecting have a bold option? Some fonts do not allow some font styles. I found this out when creating an rtb/html email composer.

Second, what you are doing isn't going to set the font style correctly. Try just this first.
Code:
 doc.Text1.SelectionFont = New System.Drawing.Font(doc.Text1.SelectionFont, FontStyle.Bold)

What are you trying to make happen with this?
Code:
doc.Text1.SelectionFont.Style Xor FontStyle.Italic

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Oooooops sorry i posted the code for italics instead of bold. Ok so i hav searched the net and found that when mixed fonts are selected than SelectionFont will be nothing so i hav to write my own code to break down the selection to get individual fonts. So I'm gonna try that and if i hav anymore problems than i'll post here.
 
This did the trick for me

Private Sub fontStyle(ByVal rtb as RichTextBox, ByVal style As FontStyle)
Dim selectionStart as Integer = rtb.SelectionStart
Dim selectionLength As Integer = rtb.SelectionLength
Dim selectionEnd as integer = selectionStart + selectionLength
Dim count as Integer = 0

For count = selectionStart to (selectionEnd - 1) Step 1
rtb.select(count,1)
rtb.selectionfont = new font(rtb.selectionfont,rtb.selectionfont.style XOR style)
Next

rtb.select(selectionStart, selectionLength)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top