We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Richtextbox problem about SelBold

william007
william007 asked
on
Medium Priority
427 Views
Last Modified: 2008-02-26
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
Comment
Watch Question

Commented:
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
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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..."

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
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

Author

Commented:
Thanks:-)

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.