Richtextbox problem about SelBold

Hi, the resulting text supposed to be
Text is <b>Highlighted</b> No Highlighted
(<b>..</b> is the portion that bold)
But the result appeared to be nothing is bold, what is the problem?

Followed is the code: (rtf is the name of richtextbox)

Private Sub Command1_Click()
Dim iStart As Integer
rtf.Text = "Text is "
iStart = Len(rtf.Text)
rtf.Text = rtf.Text & "Highlighted"
rtf.SelStart = iStart
rtf.SelLength = Len(rtf.Text) - iStart
rtf.SelBold = True
rtf.Text = rtf.Text & " No Highlighted"
End Sub
LVL 9
william007Asked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
As Preece pointed out, you are losing your formatting when you set the .Text() property:

    rtf.Text = rtf.Text & "...something..."         ' setting .Text() destroys formatting

If you want to append text to an RTB, then use code like this:

    rtf.SelStart = Len(rtf.Text)
    rtf.SelLength = 0
    rtf.SelText = "...the text to append to the RTB..."
0
 
PreeceCommented:
What is happening is that "Highlighted" is set to bold, but the next line of code resets the formatting

Private Sub Command1_Click()
Dim iStart As Integer
rtf.Text = "Text is "
iStart = Len(rtf.Text)
rtf.Text = rtf.Text & "Highlighted"
rtf.SelStart = iStart
rtf.SelLength = Len(rtf.Text) - iStart
rtf.SelBold = True                                              <------  bolded here
rtf.Text = rtf.Text & " No Highlighted"                <------  reset here
End Sub


This works with the example you gave:

Private Sub Command1_Click()
    Dim iStart As Integer
    Dim iLen As Integer
   
    rtf.Text = "Text is "
    iStart = Len(rtf.Text)
    iLen = Len("Highlighted")                            <----- get len of text to be bolded
    rtf.Text = rtf.Text & "Highlighted"
    rtf.Text = rtf.Text & " No Highlighted"          <------ append other string
    rtf.SelStart = iStart
    'rtf.SelLength = Len(rtf.Text) - iStart
    rtf.SelLength = iLen                                    <------ set length this way
    rtf.SelBold = True                                       <------ bold last

End Sub

Hope this gets you in the right direction.
Preece
0
 
PreeceCommented:
Ok, this is a better working example:

Private Sub Command1_Click()
    Dim iStart As Integer
    Dim iLen As Integer
   
    rtf.Text = "Text is "
    iStart = Len(rtf.Text)
    iLen = Len("Highlighted")
    rtf.Text = rtf.Text & "Highlighted"
    rtf.SelStart = iStart
    'rtf.SelLength = Len(rtf.Text) - iStart
    rtf.SelLength = iLen
    rtf.SelBold = True
    rtf.SelStart = Len(rtf.Text)
    rtf.SelLength = 0
    rtf.SelBold = False
    rtf.SelText = " No Highlighted"
   
   
End Sub

Preece
0
 
william007Author Commented:
Thanks:-)
0
 
william007Author Commented:
Hi, there is a continuous question here,
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21781161.html
which is about the problem encounter while appending text.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.