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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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..."

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
william007Author Commented:
Thanks:-)
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.