|
From: Mad Scientist Jr on 25 May 2005 18:36 How can I highlight some text in a RichTextBox control ? My code below makes some text blue, and some red. How can I highlight some text with yellow? rtfTest.Text = "hello" rtfTest.SelStart = 2 rtfTest.SelLength = 1 rtfTest.SelBold = True rtfTest.SelColor = vbBlue rtfTest.SelStart = 3 rtfTest.SelLength = 2 rtfTest.SelBold = False rtfTest.SelColor = vbRed 'rtfTest.BackColor = vbYellow ' this sets the entire control's background, i only want to set a few characters here + there 'rtfTest.SelBackColor = vbYellow ' alas this property doesn't exist! Thanks
From: Rick Rothstein on 25 May 2005 19:49 > How can I highlight some text in a RichTextBox control ? > > My code below makes some text blue, and some red. > How can I highlight some text with yellow? > > rtfTest.Text = "hello" > rtfTest.SelStart = 2 > rtfTest.SelLength = 1 > rtfTest.SelBold = True > rtfTest.SelColor = vbBlue > rtfTest.SelStart = 3 > rtfTest.SelLength = 2 > rtfTest.SelBold = False > rtfTest.SelColor = vbRed > 'rtfTest.BackColor = vbYellow ' this sets the entire control's > background, i only want to set a few characters here + there > 'rtfTest.SelBackColor = vbYellow ' alas this property doesn't exist! The following is taken from a couple of other posts I've offered in the past when the question of coloring the background of text in a RichTextBox (VB's control doesn't provide for this directly) which I've merged together here (which is why some of the wording between paragraphs may seem awkward or choppy). Rick - MVP I got to thinking about this problem again and came up with a super kludge (that is 'super' as in large, not fantastic<g>) that seems to work. The only proviso is that there cannot be any other highlighted text in the file. <original code not provided> Okay, the code in my previous post works fine, but I've come up with a variation that might be of interest to some of you out there. This modification allows you to highlight several different words (or text strings) with several different colors. It also provides a Clear subroutine to remove all highlighting in the RichTextBox. (The code for the two subroutines appear after my signature.) So, you might choose to highlight all of the words "programmer" in your text with a light yellow background, all of the words "Visual Basic" in light orange and all of the words "skill" in light green... all at the same time. You would do that like this (assuming CommandButton's for each highlight activation). Private Sub Command1_Click() RichTextBox1.SetFocus RichTextBoxSelBkClr RichTextBox1, "programmer", _ vbYellow, False, True End Sub Private Sub Command2_Click() RichTextBox1.SetFocus RichTextBoxSelBkClr RichTextBox1, "skill", _ &HC0FFC0, False, True End Sub Private Sub Command3_Click() RichTextBox1.SetFocus RichTextBoxSelBkClr RichTextBox1, "Visual Basic", _ &HC0E0FF, True, True End Sub Then, when you were ready to remove all of the highlights, simply execute this code Private Sub Command5_Click() RichTextBox1.SetFocus ClearRichTextBoxBkClr RichTextBox1 End Sub Although you might not want to return focus to your RichTextBox depending on whether you are allowing the user to edit any text or not. Also note that I've added a 6th parameter (it's Optional) to the RichTextBoxSelBkClr subroutine based on the observation that Steve made. The FreezeCursor argument controls whether the text scrolls to the last occurence of the word(s) being searched for or not. The default value for FreezeControl is true which means the text will Not be scrolled to the last occurence. If you want it to scroll to the last occurence, specify False for the argument. NOTE... this solution is a kludge. It doesn't properly splice highlights within highlights. I didn't figure out how to control all the back references that take place under the hood in RTF when you imbed colors within colors. Do you remember in my first post when I said "The only proviso is that there cannot be any other highlighted text in the file"? Well, that is why I said it; originally, I was only going to provide the minimum functionality to answer the posted question. Then, I decided to extend the functionality anyway. I meant to mention that proviso again along with the warning about the highlight within highlight problem... and I forgot. I'm glad you tripped over it and posted your "challenge"... it gave me a chance to add this note for the archived record. Rick - MVP Sub RichTextBoxSelBkClr(RTB As RichTextBox, _ Word As String, BkClr As Long, _ Optional MatchCase As Boolean = False, _ Optional WholeWord As Boolean = False, _ Optional FreezeCursor As Boolean = True) Dim CStart As Long Dim CEnd As Long Dim Options As Long Dim Position As Long Dim CNum As Integer Dim BkColor As String Dim EColors As String Dim RTF As String Dim CTbl() As String Dim CiLst() As String If MatchCase Then Options = rtfMatchCase If WholeWord Then Options = Options Or rtfWholeWord BkColor = ";\red" & CStr(BkClr Mod 256) & _ "\green" & CStr(BkClr \ 256 Mod 256) & _ "\blue" & CStr(BkClr \ 65536) With RTB .Visible = False .HideSelection = Not FreezeCursor Position = .Find(Word, 0, , Options) Do While Position > -1 .SelText = "\highlightx" & .SelText & "\highlight0 " RTF = .TextRTF CStart = InStr(RTF, "{\colortbl") If CStart Then CEnd = InStr(CStart, RTF, ";}") EColors = Mid$(RTF, CStart, CEnd - CStart + 2) CNum = UBound(Split(EColors, ";")) Else CNum = 1 RTF = Replace$(RTF, "{\fonttbl", _ "{\colortbl ;}{\fonttbl") End If RTF = Replace$(RTF, "\highlightx", _ "\highlight" & CStr(CNum)) CTbl = Split(RTF, "{\colortbl") CiLst = Split(CTbl(1), ";}") CiLst(0) = CiLst(0) & BkColor CTbl(1) = Join(CiLst, ";}") RTF = Join(CTbl, "{\colortbl") .TextRTF = Replace$(RTF, "\\highlight", "\highlight") Position = .Find(Word, Position + Len(Word), , Options) Loop .Visible = True End With End Sub Sub ClearRichTextBoxBkClr(RTF As RichTextBox) With RTF Do While .TextRTF Like "*\highlight1 *" RTF = Replace$(RTF, "\highlight1 ", "") RTF = Replace$(RTF, "\highlight0 ", _ "\highlight1 \highlight0 ") Loop End With End Sub
From: Veign on 26 May 2005 20:14 Highlight words in a RichTextBox http://www.veign.com/vrc_codeview.asp?type=app&id=28 -- Chris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp -- Read. Decide. Sign the petition to Microsoft. http://classicvb.org/petition/ "Mad Scientist Jr" <usenet_daughter(a)yahoo.com> wrote in message news:1117060584.440492.73070(a)g43g2000cwa.googlegroups.com... > How can I highlight some text in a RichTextBox control ? > > My code below makes some text blue, and some red. > How can I highlight some text with yellow? > > rtfTest.Text = "hello" > rtfTest.SelStart = 2 > rtfTest.SelLength = 1 > rtfTest.SelBold = True > rtfTest.SelColor = vbBlue > rtfTest.SelStart = 3 > rtfTest.SelLength = 2 > rtfTest.SelBold = False > rtfTest.SelColor = vbRed > 'rtfTest.BackColor = vbYellow ' this sets the entire control's > background, i only want to set a few characters here + there > 'rtfTest.SelBackColor = vbYellow ' alas this property doesn't exist! > > > Thanks >
From: Mad Scientist Jr on 27 May 2005 09:07 Works like a charm - thanks ! Rick Rothstein wrote: > > How can I highlight some text in a RichTextBox control ? > Okay, the code in my previous post works fine, but I've come up with a > variation that might be of interest to some of you out there.
|
Pages: 1 Prev: Ole Idispatch exception code 0 from Microsoft word Next: Print to a Barcode Printer |