-
Jun 10th, 2021, 05:03 AM
#1
Thread Starter
New Member
[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
-
Jun 10th, 2021, 06:06 AM
#2
Re: How to bold selected words in RichTextBox?
Use the .SelStart, .SelLength, .SelText and .SelBold properties
-
Jun 10th, 2021, 06:20 AM
#3
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.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE 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.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jun 10th, 2021, 08:43 AM
#4
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).
-
Jun 10th, 2021, 10:11 AM
#5
Thread Starter
New Member
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.
-
Jun 10th, 2021, 11:28 AM
#6
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"
-
Jun 10th, 2021, 02:29 PM
#7
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
-
Jun 10th, 2021, 04:36 PM
#8
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" & "}"
-
Jun 10th, 2021, 04:54 PM
#9
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.
-
Jun 10th, 2021, 09:21 PM
#10
Thread Starter
New Member
Re: How to bold selected words in RichTextBox?
Originally Posted by dilettante
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|