Results 1 to 10 of 10

Thread: [RESOLVED] How to bold selected words in RichTextBox?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2021
    Posts
    8

    Resolved [RESOLVED] How to bold selected words in RichTextBox?

    Hi, I'm new in RichTextBox. I want to select a word that wrapped between my special symbol I self invented (#B#) to be bold and deleted the symbol after bold, how to do that?

    Code:
    RichTextBox1.Text = "There is a #B#Little#B# cat."
    RichTextBox1.Font.Size = 12
    RichTextBox1.Font.Name = "Courier New"
    Expected results:
    There is a Little cat

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,512

    Re: How to bold selected words in RichTextBox?

    Use the .SelStart, .SelLength, .SelText and .SelBold properties

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,506

    Re: How to bold selected words in RichTextBox?

    In a loop:
    Search for the (first/next) occurence of "#B#".
    if it's the opening Tag, Add 3 to the result, and save it. i'd use a dynamic 2D-Array, with the "start" saved in the first dimension
    From this "new" starting point search for the next occurence of "#B#". Save the result in the second dimension of the Array.
    Now (!) add 3 to that result for your next starting point.
    To signify a found "#B#'" as an opening Tag i'd use a boolean Variable i toggle with each new occurence and use that boolean to know when to add 3 to the result (before/after saving the value)
    Loop end
    In a new loop LBound(Array) to UBound(Array)
    Set Font to Bold using the Start and End-Values in the Array using the Properties Arnout mentioned above
    Loop End
    Finally: Replace all "#B#" with ""/vbNullString

    EDIT: And no, i'm not going to step into the minefield of discussions like
    "Why not use html-tags", or "this is so slow" or any other reason.
    This above is the most simple algorithm to skin that cat, which i would expect even a rookie in any programming language to come up with
    Last edited by Zvoni; Jun 10th, 2021 at 06:28 AM.
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,839

    Re: How to bold selected words in RichTextBox?

    If I were you I would un-invent #B# and instead, invent a start location of #B# and and end location of #/B". HTML and other 'languages' tend to use the slash "/" to indicate the END of formatted text. Will make your search in your RTB much easier.

    Sammi

    EDIT: Or like Z said....use existing practices.
    Sam I am (as well as Confused at times).

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2021
    Posts
    8

    Re: How to bold selected words in RichTextBox?

    First of all, thanks for Zvoni's explanation, but I think a code sample would help me understand better because I'm still new in vb too.

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,209

    Re: How to bold selected words in RichTextBox?

    Code:
    RichTextBox1.SelText = "There is a "
    RichTextBox1.SelBold = True
    RichTextBox1.SelText = "little"
    RichTextBox1.SelBold = False
    RichTextBox1.SelText = " cat"

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,302

    Re: How to bold selected words in RichTextBox?

    Something like:

    Code:
        Dim LPos As Long
        Dim RPos As Long
    
        With RichTextBox1
    '        .Text = "There is a #B#Little#B# cat.  #B#Meow.#B#  I can hear it!"
    '                        /- empty                            /- adjacent      /- dangling
            .Text = "There#B##B# is a #B#Little#B# cat.  #B#Me#B##B#ow.#B#  I can #B#hear it!"
            Do
                LPos = .Find("#B#", LPos, , rtfMatchCase Or rtfNoHighlight)
                If LPos >= 0 Then
                    .SelStart = LPos
                    .SelLength = Len("#B#")
                    .SelText = vbNullString
                    RPos = .Find("#B#", LPos, , rtfMatchCase Or rtfNoHighlight)
                    If RPos >= 0 Then
                        .SelStart = RPos
                        .SelLength = Len("#B#")
                        .SelText = vbNullString
                    Else
                        RPos = &H7FFFFFFF 'No right tag, so through the end of all text.
                    End If
                    If RPos - LPos > 0 Then
                        .SelStart = LPos
                        .SelLength = RPos - LPos
                        .SelBold = True
                    End If
                    LPos = RPos
                End If
            Loop While LPos >= 0
            .SelStart = 0
        End With

  8. #8
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,209

    Re: How to bold selected words in RichTextBox?

    Instead of using your custom tagging why don't you write the RTF codes directly?

    Code:
    RichTextBox1.TextRTF = "{\rtf1\fs" & RichTextBox1.Font.Size * 2 & " " & "There is a \b little\b0  cat" & "}"

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,302

    Re: How to bold selected words in RichTextBox?

    I suspect the idea is to let users manually enter the desired markup and then convert it.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jun 2021
    Posts
    8

    Re: How to bold selected words in RichTextBox?

    Quote Originally Posted by dilettante View Post
    Something like:

    Code:
        Dim LPos As Long
        Dim RPos As Long
    
        With RichTextBox1
    '        .Text = "There is a #B#Little#B# cat.  #B#Meow.#B#  I can hear it!"
    '                        /- empty                            /- adjacent      /- dangling
            .Text = "There#B##B# is a #B#Little#B# cat.  #B#Me#B##B#ow.#B#  I can #B#hear it!"
            Do
                LPos = .Find("#B#", LPos, , rtfMatchCase Or rtfNoHighlight)
                If LPos >= 0 Then
                    .SelStart = LPos
                    .SelLength = Len("#B#")
                    .SelText = vbNullString
                    RPos = .Find("#B#", LPos, , rtfMatchCase Or rtfNoHighlight)
                    If RPos >= 0 Then
                        .SelStart = RPos
                        .SelLength = Len("#B#")
                        .SelText = vbNullString
                    Else
                        RPos = &H7FFFFFFF 'No right tag, so through the end of all text.
                    End If
                    If RPos - LPos > 0 Then
                        .SelStart = LPos
                        .SelLength = RPos - LPos
                        .SelBold = True
                    End If
                    LPos = RPos
                End If
            Loop While LPos >= 0
            .SelStart = 0
        End With
    Well done, dilettante, is working so well. Thank you very much for your effort.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width