martin2k

ForumsVB6GamesSoftwareDirectoryHTMLArticlesBlogContact

Richtextbox Hyperlink Control


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

ActiveX Control

I made this control for my NoteBook program so that uniform resource locators (URLs) and other links can be appear and work like they do in plain text e-mails in Microsoft Outlook.  They will appear blue and underlined by default and when clicked on, the relevant program will open e.g. when an http: link is clicked on, a browser window will appear with that link navigated to.  The control can be downloaded here.

This control only works with the Microsoft Rich Textbox Control.

Currently, these are the supported protocols:

about:
file:
ftp:
gopher:
http:
https:
mailto:
news:
telnet:
www. - 'http:' will be used as a prefix when URLs displayed like this are clicked on

This control is more suited to RichTextBoxes where the Locked property is set to True, otherwise the RichTextBox has to be reformatted each time the text changes.

Properties

URL

Read-only.  Returns the URL generated by the RichWordOver event.  Can be used in a status bar, for example, to show what link will be navigated to when the mouse is over a link in the RichTextBox.

URLBold

Allows you to set whether the URLs will appear bold or not.  Default is False.

URLColour

Sets the colour of the URLs.  Default is blue.

URLItalic

Allows you to set whether the URLs will appear italicised or not.  Default is False.

URLUnderline

Allows you to set whether the URLs will appear underlined or not.  Default is True.

Methods

RichWordOver

frm As Object, rch As Object, x As Single, y As Single

Changes the mouse cursor into a hand to indicate that a URL can be clicked on.  This method must be used in the RichTextBoxes MouseMove event:

Private Sub RichTextBox1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Hyperlink1.RichWordOver Me, RichTextBox1, x, y
End Sub

URLFormat

rch As Object

Formats the RichTextBox so that URLs are displayed as determined by the properties.  E.g. by default, URLs will be displayed underlined in blue:

A RichTextBox after URLFormat
A RichTextBox after URLFormat

Please note that the RichTextBox will need formatting every time the text changes.

URLLaunch

Launches the URL the mouse cursor is currently over.  It only works when the RichWordOver method has been used.  It should be used in the RichTextBoxes Click event:

Private Sub RichTextBox1_Click()
Hyperlink1.URLLaunch
End Sub

Events

URLChanged

Is called when the URL property changes.


blog comments powered by Disqus

Archived Comments

From: Azizah
Date: Wednesday, April 20, 2011 at 02:15:25
Comments: it is very good example. i like it
 
From: Abhinav Kumar
Date: Friday, February 8, 2008 at 16:55:34
Comments: what should i do if i has to use on a command box. it means that when i click on command box the hyperlink should start to work.
 
From: Muthu
Date: Monday, November 12, 2007 at 14:01:19
Comments:

When i use Hyperlink1.URLFormat RichTextBox1 the system gets hanged. Im using Windows XP SP 2

 
Reply: If your document uses a very large amount of links, it can take a bit of time to format all of them.  Do you think this is causing the problem?
 
From: pia
Date: Monday, November 5, 2007 at 12:22:08
Comments: it is very good example of rich text hyperlink
 
From: Dean
Date: Tuesday, April 17, 2007 at 12:10:32
Comments: hyperlink.ocx is very good but seems to have a problem running under Vista. It gets an error "call to register server dll failed 0x80004005" when you try to register the ocx. Has anyone else come up against this or know of a solution?

Thanks
Dean

 
Reply: This control uses API functions from shell32.dll and user32.dll.  I can only imagine that this maybe causing a problem.

I have heard that some people have had problems using VB6 with Windows Vista, but as I don't have it myself, I can't verify this.

Has anyone else had problems with this control in Vista?

 
From: Ruturaaj
Date: Thursday, January 11, 2007 at 16:00:54
Comments: Hello!

Is it possible to launch a URL for a Text which is not the URL itself? For example, User clicks the text, say "Click Here" and a URL is launched ... is it possible in RichTextBox?

 
Reply: URLs can only be clicked on if prefixed with the protocols mentioned above.  I haven't programmed the control to work in the way you have mentioned.
 
From: Woodas Lai
Date: Monday, October 2, 2006 at 16:21:28
Comments: Hi ... the control is great. However, when I type some chinese characters plus a link ... it seems that the hyperlink control cannot parse the text successfully. Is it true that it does not work with chinese character? Or does it? Thanks.

Woodas Lai

 
Reply: Unfortunately I did not program this control to recognise Chinese characters.
 
From: john sternig
Date: Monday, July 18, 2005 at 02:32:02
Comments: Really nice control! I used the "minimum code" as shown in response to another users question and the hyperlinks all work, but they are not displayed in blue or underlined, even though both properties are set to true. Do you have any idea how to correct this? Thanks.
 
Reply: I have just noticed a mistake in the code you were looking at, which has now been corrected.  The URLFormat method must be executed each time the text in the RichTextBox changes.

If you load a file into the RichTextBox, you will need to run the URLFormat method afterwards for the underlined blue text to appear.

 
From: Asad
Date: Friday, May 6, 2005 at 12:23:50
Comments: Dude, that's a very nice control indeed. But is it possible for user to add or remove URL's ? I would like to add a Url named "Datum:". Is that possible ?

Thanks a lot
Asad.
The Netherlands

 
Reply: Unfortunately this is not currently possible, but its something I was thinking of adding at some point.
 
From: Peter F Van Peborgh
Date: Tuesday, February 15, 2005 at 16:31:08
Comments: "Private Sub RichTextBox1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)"

gives me the error:

"Procedure declaration does not match description of event or procedure having the same name".

Also, the Hyperlink is labelled "Hyperlink0", not "1" but I have corrected this throughout the code you give.

Any idea on the compilation error message?

Thanks.

 
Reply: It could be that you have a different version of the RichTextBox control to me, which has different arguments.  Either try installing SP6 or if you don't want to do this, try allowing Visual Basic to generate the code instead of copying mine.  Do this by first selecting 'RichTextBox1' in the Object list box in the Code window and then by choosing the 'MouseMove' event in the Procedure list box.
 
From: Scott Downey
Date: Friday, December 24, 2004 at 21:54:27
Comments: Very nice control indeed! Can you set it to release the Y coordinate so the hand reverts back to normal and box clicks dont run the hyperlink when the cursor falls below the text in the richtextbox to empty lines? The x 'horizontal movement releases fine. When there is no more text under a hyperlink, someone clicking on blank areas underneath the hyperlink executes the url load event. And selecting text holding down a mouse button, if the button is released under the hyperlink in a blank area launches the url event.
 
Reply: I'm glad you like the control.

At the time of making it, I could not work out how to overcome the problems you have mentioned.  One way around the problem of highlighting a URL is to put the URLLaunch event into the RichTextBox's MouseDown event.  Therefore if the user starts selecting text and finishes selecting in a URL, the URL will not be launched.

If you try typing a URL into Microsoft Outlook 2000 (in a plain text message), you will see that the cursor stays as the hand when the cursor is under the URL if there is only a URL in the message.  So the Outlook programmers could not find a way around this either.

 
From: Greg
Date: Wednesday, July 21, 2004 at 13:07:25
Comments: In your example you have a textbox showing different  URL's. However when I tried this, only the last URL launched whichever URL I clicked on.  How do you set your control to open different URL's?
 
Reply:

This is the minimum code you need in your program to get this control to work properly:

Private Sub RichTextBox1_Change()
Hyperlink1.URLFormat RichTextBox1
End Sub

Private Sub Hyperlink1_URLChanged()
Caption = Hyperlink1.URL
End Sub


Private Sub RichTextBox1_Click()
Hyperlink1.URLLaunch
End Sub


Private Sub RichTextBox1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Hyperlink1.RichWordOver Me, RichTextBox1, x, y
End Sub

Please note that you should use the URLFormat method each time the text in the RichTextBox changes.

 
From: Steve
Date: Sunday, June 13, 2004 at 14:28:18
Comments: Is the source code of the Richtextbox Hyperlink Control available? Thanks in advance.
 
Reply: I do not give away source code for my compiled programs or ActiveX controls.  I learned how to make this control by looking at projects on the VB Helper website.  I'm sure other websites such as Planet Source Code will also show you how to do it.
 
From: Henkivartija
Date: Friday, February 27, 2004 at 13:58:19
Comments: Hi!

Richtextbox Hyperlink Control is very good, but unfortunally it does not work with spaces... Or does it ?
So is there any way to make hyperlink work with spaces?

This one is not working
file:c:\documents and settings

and this one works fine in many windows applications...
<file:c:\documents and settings>

Best wishes,
Henkivartija

 
Reply: Unfortunately I could not work out a way of coding this control to recognise spaces i.e. determine where the ends of the URL is if it contains spaces.  One way around it is to use '%20' instead of a space e.g.:

file://c:\documents%20and%20settings


Martin Allen 1999 - 2012.  Last updated Sunday 30 December 2012 01:56:28 PM -0000.