Rich Text Box Tricks and Tips - VBForums
Rich Text Box Tricks and Tips - VBForums
Visual Basic Code from FreeVBCode Get and Set Word file Attributes with VB.NET Calculate Age in Visual Basic 2005, Counting Leap years Spatial Matrix Memory Game Introduction to Cryptoanalysis Linked List implementation in Visual Basic Submit your code to FreeVBCode
VBForums > VBForums C odeBank > C odeBank - Visual Basic 6 and earlier
#1
RichTextBox Tricks and Tips
moeur
Old Member
The following posts contain a few things that you can do with RichTextBoxes that you might not have known that you could do. If any of you know of other non-standard things that can be done with RichTextBoxes, feel free to add to this list.
Highlight text SuperScript and SubScript Insert tables Insert Pictures Find and Replace Common Dialog Spell Checking class Get Cursor Line and Column Position Detect and respond to hyperlinks WYSIWYG printing (msdn) Insert Hyperlinked text Add animated GIF's to your RichTextBox
Last edited by moeur; Mar 4th, 2008 at 10:12 AM.
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
#2
Highlighting text
moeur
Old Member
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
www.vbforums.com/showthread.php?t
1/17
7/24/2011
Chr(&H81)
Notice that in addition to inserting the \highlight tags I also insert \up# tags. This is so that I can check to see if a selection is highlighted by querying the .SelCharOffset function. This routine relies on the following function that adds a new color to the RTF color table
C ode:
Function AddColorToTable(strRTF As String, lColor As Long) As Integer Dim iPos As Long, jpos As Long Dim Dim Dim Dim Dim Dim Dim Dim ctbl As String tagColors nColors As Integer tagNew As String i As Integer iLen As Integer split1 As String split2 As String 'make new color into tag tagNew = "\red" & CStr(lColor And &HFF) & _ "\green" & CStr(Int(lColor / &H100) And &HFF) & _ "\blue" & CStr(Int(lColor / &H10000)) 'find colortable iPos = InStr(strRTF, "{\colortbl") If iPos > 0 Then 'if table already exists jpos = InStr(iPos, strRTF, ";}") 'color table ctbl = Mid(strRTF, iPos + 12, jpos - iPos - 12) 'array of color tags tagColors = Split(ctbl, ";") nColors = UBound(tagColors) + 2 'see if our color already exists in table For i = 0 To UBound(tagColors) If tagColors(i) = tagNew Then AddColorToTable = i + 1 Exit Function
Last edited by moeur; Mar 12th, 2007 at 07:54 PM.
#3
Super and Subscripting
moeur
Old Member
Two other functions that the RichTextBox control does not gives us are super and subscripting. As before we can accomplish this by inserting RTF code. Notice again that I also add \up0 and \dn0 tags so that I can determine if text has been subscripted by querying the .SelCharOffset property.
www.vbforums.com/showthread.php?t
2/17
7/24/2011
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703 C ode:
#4
Insert Tables
moeur
Old Member
Another useful functionality that can be added to the RichTextBox controls is the ability to insert tables. The RichTextBox controls support a limited subset of the table related Rich Text Format tags, but none of that is made accessible to users of the control. I've attached a class that you can use to insert tables into your RichTextBox controls.
Join Date: Nov 04 Properties - all sizes are in twips Location: In Hiding.... Weather: xLeft - Position of the left edge of the table sizzzzlin'........ C ode: Secret isCentered - Set to True to center the table Posts: 2,703
Rows - Sets or returns the number of rows in the table Columns - Sets or returns the number of columns in the table Row - An Array of Rows (1 to Rows) Column - An Array of columns (1 to Columns) Column(i).xWidth - Width of the ith column Cell - A 2-d Array of Cells (1 to Rows, 1 to Columns) Cell(r, c).Contents - Sets or returns the contents of the cell
Methods InsertTable(RTB As RichTextBox) - Inserts the table into the RichTextBox at the currrent cursor position.
An example of use is
C ode:
www.vbforums.com/showthread.php?t
3/17
7/24/2011
Option Explicit
table
'Row 2 .Cell(2, 1).Contents = "Row2" .Cell(2, 2).Contents = "R2C2" .Cell(2, 3).Contents = "R2C3" 'do we want to center it on the page? .isCentered = True 'insert the table at the current cursor postion
Attached Files RTFtable.zip (5.3 KB, 7028 views)
#5
Insert Pictures
moeur
Old Member
There are several ways to insert pictures into a RichTextBox control. This is one method that does not rely on the clipboard, but does use some metafile stuff. Here is the routine to insert the picture
C ode:
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
'Inserts the picture at the current insertion point Public Function InsertPicture(RTB As RichTextBox, pic As StdPicture) Dim strRTFall As String Dim lStart As Long With RTB .SelText = Chr(&H9D) & .SelText & Chr(&H81) strRTFall = .TextRTF strRTFall = Replace(strRTFall, "\'9d", PictureToRTF(pic)) .TextRTF = strRTFall 'position cursor past new insertion lStart = .Find(Chr(&H81)) strRTFall = Replace(strRTFall, "\'81", "") .TextRTF = strRTFall .SelStart = lStart End With End Function
Here is the routine that converts the picture into an RTF string
C ode:
www.vbforums.com/showthread.php?t
4/17
7/24/2011
#6
Find and Replace Common Dialog
moeur
Old Member
Until recently I didn't know that you could access the "Find-And-Replace" Common Dialog. Here is a class that makes it easy to access. The class will work with either a standard TextBox or a RichTextBox. Here is an example of how you might use the class.
C ode:
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
Option Explicit 'declare with events so that we can override the default 'behavior of the class and/or handle ShowHelp Dim WithEvents FindDialog As clsFindandReplace Private Sub Form_Load() Set FindDialog = New clsFindandReplace End Sub Private Sub Command2_Click() 'show the Find and Replace dialog box 'pass the handle of our RichTextBox to 'the class FindDialog.ShowReplace RTB.hwnd End Sub
#7
www.vbforums.com/showthread.php?t
5/17
7/24/2011
moeur
Old Member
Here is a class that provides full spell checking functionality for the RichTextBox. This class has only two methods:
GetSpellingErrors checks the spelling in all the text of a RTB and returns the number of spelling errors found and marks then all. A right-click on any error brings up a popup menu with suggested changes. If the user selects a change from the menu, then the replacement is made. ClearSpelling clears all the marked errors.
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
Option Explicit Private SpellCheck As clsSpellCheck Private Sub cmdSpellCheck_Click() SpellCheck.GetSpellingErrors RTB End Sub Private Sub cmdStopSpell_Click() SpellCheck.ClearSpelling End Sub Private Sub Form_Load() Set SpellCheck = New clsSpellCheck RTB.LoadFile App.Path & "\recipe.rtf" End Sub
Attached is the source code for all the above mentioned items PLUS a free mispelled recipe!
Attached Files SpellC heck.zip (18.3 KB, 2692 views)
#8
Get row and column number of cursor
moeur
Old Member
Here is some simple code that will give you the cursor position in a RichTextBox, It gives you the line number and the column number of the cursor. Attached is a project that demonstrates use of the routine.
C ode:
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
www.vbforums.com/showthread.php?t
6/17
7/24/2011
Option Explicit
#9
Auto Detect and respond to URLs
moeur
Old Member
The RichTextBox control has the ability to detect URLs as they are typed. It can convert this text into a hyperlink which can launch a browser when clicked. To turn on Auto URL detection simply send the RTB an EM_AUTOURLDETECT message.
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
When the control detects that a URL is being entered, it reformats the text being entered so that it looks like a hyperlink and marks that text with a CFE_LINK effect. When the mouse pointer is over text with a CFE_LINK effect, the RTB can be configured to send a message to its parent. In order to respond to mouse events over the hyperlink text, the parent has to be subclassed or hooked. The following code shows how to setup Auto URL detection
C ode:
Public Sub EnableAutoURLDetection(RTB As RichTextBox) 'enable auto URL detection SendMessage RTB.hwnd, EM_AUTOURLDETECT, 1&, ByVal 0& 'subclass the parent of the RTB to receive EN_LINK notifications Set FormSubClass = New clsSubClass FormSubClass.Enable RTB.Parent.hwnd 'set RTB to notify parent when user has clicked hyperlink SendMessage RTB.hwnd, EM_SETEVENTMASK, 0&, ByVal ENM_LINK End Sub
And to respond to a left mouse click you could do the following in your form's subclass routine.
C ode:
www.vbforums.com/showthread.php?t
7/17
7/24/2011
#10
Re: RichTextBox Tricks and Tips
longwolf
Frenzied Member
Wow, you have some really great stuff here! But I see one major draw back. In SpellCheck.zip you have a dll named DBGWPROC.DLL. Its properties say: You have a license to use this file only if you have a copy of the book. You may not redistribute this file.
#11
Re: RichTextBox Tricks and Tips
moeur
Old Member
The DbgWProc.Dll is only used for debugging purposes so doesn't really need to be included, but the author ( Matthew Curland) has given his permission to redistribute it. The file is freely available many places around the iternet.
Last edited by moeur; Oct 6th, 2005 at 12:03 PM.
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
#12
Re: RichTextBox Tricks and Tips
JustinW
New Member Join Date: Jan 06 Posts: 2
Hi: First off, thanks so much for posting the codes for inserting tables. That's much appreciated!!!
www.vbforums.com/showthread.php?t
8/17
7/24/2011
#13
Re: Insert Tables
darki
New Member Join Date: Apr 06 Posts: 3
1. Public Sub InsertTable(RTB As RichTextBox) 2. 'set column widths 3. For c = 1 To mvarColumns 4. strInsert = strInsert & "\cellx" 5. w = mvarxLeft 6. For i = 1 To c 7. [color=Red]w = w + mvarclsColumn(c).xWidth[/color] 8. Next i 9. strInsert = strInsert & CStr(w) 10. Next c
If you have all columns same size you can't see difference, but must be: w = w + mvarclsColumn(i).xWidth
#14
Re: RichTextBox Tricks and Tips
moeur
Old Member
Thanks darki
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
#15
Re: RichTextBox Tricks and Tips
mamaco
New Member Join Date: May 06 Posts: 1
Hi All I have strange problems with the syntax highlighting programming No.1 Sendmessage is often out of work '---- ------------------Quotation----------------- ----LN = SendMessage(RTB.hwnd, EM_LINEINDEX, Byval LineNum, 0&) '---- ------------------Quotation----------------- ----this works fine in ANSI mode(english text),but when I use UNICODE mode,it returns wrong result as always. I have to seek the preview enter key to locate the fst pos of a line. No.2 '---- ------------------Quotation----------------- ----.SelStart
www.vbforums.com/showthread.php?t
9/17
7/24/2011
#16
Re: RichTextBox Tricks and Tips
BodwadUK
KING BODWAD XXI
Maybe I am confused but I am trying to highlight text on the go without changing the cursor position. Is there anyway to get the RTF text position? Selstart only has it for the standard text and I need the rtf selstart so that I can insert my own colour tags before and after my word. Just in case your wondering I am writing a script editor and the cursor jumps the box around whenever I highlight words __________________ If you dribble then you are as mad as me Lost World Creations Website (Astrophotography) Lene Marlin
#17
Re: RichTextBox Tricks and Tips
moeur
Old Member
Probably the easiest thing to do is lock the window while you are doing the highlighting with LockWindowUpdate.
VB C ode:
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
Private Declare Function LockWindowUpdate Lib "user32" ( _ ByVal hwndLock As Long) As Long Private Sub Command1_Click() Dim iPos As Integer 'save the current cursor position iPos = RTB.SelStart 'prevent the window from changing LockWindowUpdate RTB.hWnd 'highlight a word RTB.Find "is" HighLight RTB, vbYellow 'restore the cursor position RTB.SelStart = iPos 'unlock the window LockWindowUpdate 0 End Sub
#18
Re: RichTextBox Tricks and Tips
BodwadUK
KING BODWAD XXI
Thanks I shall give it a go __________________ If you dribble then you are as mad as me Lost World Creations Website (Astrophotography) Lene Marlin
#19
Re: RichTextBox Tricks and Tips
BodwadUK
KING BODWAD XXI
Thats seems to do it thanks. I had it on the main form hwnd before but changing it to the hwnd of the rich text box itself seems to do the trick thanks __________________ If you dribble then you are as mad as me
www.vbforums.com/showthread.php?t
10/17
7/24/2011
Join Date: Aug 02 Location: Nottingham Posts: 2,164
#20
Re: RichTextBox Tricks and Tips
nokmaster
Fanatic Member Join Date: Nov 02 Location: Philippines Posts: 877
#21
Re: RichTextBox Tricks and Tips
moeur
Old Member
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
#22
Re: RichTextBox Tricks and Tips
nokmaster
Fanatic Member Join Date: Nov 02 Location: Philippines Posts: 877
@moeur hi, i mean richtextbox with vertical scroller. if u put this command into command1
VB C ode:
1. with rtb 2. .selcolor = vbblack 3. .seltext = "ok" & vbcrlf 4. end with
it will scroll down right? but if you scroll this scroller to up then press the button again it will not scroll.
#23
Re: RichTextBox Tricks and Tips
BodwadUK
KING BODWAD XXI
change selstart to the location you want the cursor. It should scroll for you __________________ If you dribble then you are as mad as me Lost World Creations Website (Astrophotography) Lene Marlin
#24
Re: RichTextBox Tricks and Tips Quote:
nokmaster
Fanatic Member Join Date: Nov 02 Location: Philippines Posts: 877
Originally Posted by BodwadUK change selstart to the location you want the cursor. It should scroll for you how?
www.vbforums.com/showthread.php?t
11/17
7/24/2011
Jun 13th, 2006, 05:01 AM
BodwadUK
KING BODWAD XXI
you mean it doesnt scroll down until you hit the bottom of the text window with your cursor? __________________ If you dribble then you are as mad as me Lost World Creations Website (Astrophotography) Lene Marlin
#26
Re: RichTextBox Tricks and Tips
Yuji1
New Member Join Date: Jun 06 Posts: 1
I wish to know how to, uh, well, basically, I am building a chat program for the Hell of it, but want colored text in a RichTextBox. BUT, I dunno how to do it, cause when I try to switch to a color, it ****s up. Ya, so, help?
#27
Re: RichTextBox Tricks and Tips
rack
Fanatic Member Join Date: Jul 06 Location: Anchorage, Alaska Posts: 545
When I try to set the xwidth to 3.5 it dosen't not draw a table, the text is all scambled. I need the width of each cell to be 3.5inch I need the Height of each cell to be 2inch I need there to be 2 columns, with 5 rows. What is the best way to accomplish this? EDIT: I got the width correct, I was using 3.5, when I should have been using Twips, 5040. How do I do the Height of each cell to be exactly 2inches? Or 2880 Twips. __________________ Please RATE posts, click the RATE button to the left under the Users Name. Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I se e the world I wishe d tonight, ne ve r the le ss, sle e p has com e , and de ath shall soo n follow..." 1998 Je re m y J Swartwood
#28
Re: RichTextBox Tricks and Tips
MartinLiss
Former Admin/Moderator
Using your example I've come to understand how to have text in a richtextbox act as a hyperlink, but given text that looks like this (from which I will strip away the URL tags) [ URL=http://www.vbforums.com/showthread.php?p=11111]this thread[/URL ]
Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
2009, 2010, 2011 If someone helped you today then please consider rating their post.
www.vbforums.com/showthread.php?t
12/17
7/24/2011
#29
Re: RichTextBox Tricks and Tips
moeur
Old Member
All you need to do is mark the text that you want to attach a hyperlink to with the CFE_LINK Effect. You'll have to keep the URL in a list somewhere so you can respond to user mouse clicks in your WM_NOTIFY event interception. See my cool spell checker example for how to do this. The spell checker marks all mispelled words with the CFE_LINK effect so that when the user right clicks on it spelling suggestions can be made.
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
BTW this is much better than RobDogg's simple little spell checker Edit: the link you provided above does not work. -Bill
#30
Re: RichTextBox Tricks and Tips
MartinLiss
Former Admin/Moderator
Thanks for the information Bill. I know about the link; I intentionally made it invalid. BTW, did you get the couple of emails I sent you? __________________
Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
2009, 2010, 2011 If someone helped you today then please consider rating their post.
#31
Re: RichTextBox Tricks and Tips
MartinLiss
Former Admin/Moderator
I just downloaded the clsSpellCheck example and I ran into a problem. When I run it I see the recipe (which I've made previously BTW ). I click Spell Check and it underlines all the misspellings. However when I double-clicked one of them nothing happened, so I clicked Spell Check again and got an Invalid property value error in this line
VB C ode:
1. 'find each misspelling in the document 2. For Each spError In WordDoc.SpellingErrors 3. iPos = mRTB.Find(spError, iPos + 1, , rtfWholeWord Or rtfMatchCase) 4. [hl="#FFFF80"]mRTB.SelStart = iPos[/hl]
in GetSpellingErrors. __________________
Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
2009, 2010, 2011 If someone helped you today then please consider rating their post.
Last edited by MartinLiss; Feb 4th, 2007 at 06:02 PM.
www.vbforums.com/showthread.php?t
13/17
7/24/2011
Feb 4th, 2007, 07:48 PM
MartinLiss
Former Admin/Moderator
Okay I have the word "this" in my example above formatted with the CFE_LINK effect and I have the associated URL stored in a collection and the richtextbox is enabled for AutoURLDetection. How do I actually get the RTB to open the browser to the stored URL? I assume I have to do something in the RTB's Click event, but what? __________________
Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
2009, 2010, 2011 If someone helped you today then please consider rating their post.
#33
Re: RichTextBox Tricks and Tips
MartinLiss
Former Admin/Moderator
I was able to hammer out a way to do it but I'd still be interested in the right way. __________________
Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
2009, 2010, 2011 If someone helped you today then please consider rating their post.
#34
Re: RichTextBox Tricks and Tips
moeur
Old Member
The spell checker requires that you right click on a word not double click. To respond to the user clicking on your hyperlink, see the AutoURL example above. when text in an RTB has its CFE_LINK effect set, the text will be blue and underlined. More importantly, the RTB will send a WM_NOTIFY message to its parent form for certain mouse operations on the text.
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
To respond to these messages (such as a left click) you have to subclass the parent form and intercept these messages. So, 1. see the spell check example to see how to set the CFE_LINK effect for text. 2. See the AutoURLDetect example to see how to bring up the browser (or whatever action) when the user clicks on your special text. And I did receive your email and even responded. -Bill
#35
Re: RichTextBox Tricks and Tips
MartinLiss
Former Admin/Moderator
Thanks, I've done all that and I basically have it working. I do have a problem though. Take a look at my post #28. You see that I'm substituting the "Script prompt" that you optionally enter when you insert a hyperlink in a post for the URL itself, so when the user clicks on the underline-blue word I need to tell VB somehow what the URL associated with that word is. I've worked that out by storing both pieces of data in a modified version of your
www.vbforums.com/showthread.php?t
14/17
7/24/2011
Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
2009, 2010, 2011 If someone helped you today then please consider rating their post.
#36
Re: RichTextBox Tricks and Tips
moeur
Old Member
Ok I understand your question now. I thought it was strange that I was having to explain things to you that are spelled out in the example. Here is an idea: This \v www.vbforums.com \v0
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
#37
Re: RichTextBox Tricks and Tips
MartinLiss
Former Admin/Moderator
What if there were two URLs and I needed to be able to differentiate them? __________________
Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
2009, 2010, 2011 If someone helped you today then please consider rating their post.
#38
Re: RichTextBox Tricks and Tips
moeur
Old Member
My idea is to insert your URL right in the RTF text and hide it with the \v tags. You can then retrieve this info when the user clicks the adjacent hyperlink. Does this not work?
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
www.vbforums.com/showthread.php?t
15/17
7/24/2011
Feb 5th, 2007, 05:25 PM
MartinLiss
Former Admin/Moderator
No, that seems like a great idea! I had no idea (until now) what the \v tag did. Is there a comprehesiuve list someplace of rtf tags? __________________
Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
2009, 2010, 2011 If someone helped you today then please consider rating their post.
#40
Re: RichTextBox Tricks and Tips
moeur
Old Member
Here is the specification of RTF 1.5 http://www.biblioscape.com/rtf15_spec.htm Note however that the richtextbox control supports only about 10% of the codes. I use the vbforums editor to test codes. Go to View\RTF Then enter the codes you want to test Then select View\Normal to see if it worked. If it didn't work and you go back to View/RTF and the code you entered is gone, then the control does not support it so it removed it.
Join Date: Nov 04 Location: In Hiding.... Weather: sizzzzlin'........ C ode: Secret Posts: 2,703
Page 1 of 4 1 2 3 4 >
Previous Thread | Next Thread VBForums > VBForums C odeBank > C odeBank - Visual Basic 6 and earlier
Posting Rules
You You You You may may may may not not not not post new threads post replies post attachments edit your posts
BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
All times are GMT -5. The time now is 10:33 AM. Contact Us - VB Forums - Archive - Top
MARKETPLACE
Rosetta Stone: New Mobile App for iPad
Ge t Full Acce ss AND the C om ple te 1-5 Le ve l Se t for just $479 plus FR EE Shipping! www.Rose ttaStone .com
www.vbforums.com/showthread.php?t
16/17
7/24/2011
Search:
About Internet.com Copyright 2011 QuinStreet Inc. All Rights Reserved. Legal Notices, Licensing, Permissions, Privacy Policy. Advertise | New sletters | E-mail Offers
Powered by vBulletin Version 3.8.1 C opyright 2000 - 2011, Jelsoft Enterprises Ltd.
www.vbforums.com/showthread.php?t
17/17