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.
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
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
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?
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.
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.
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.
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.
"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.
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.
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 - 2011. Last updated
Wednesday 10 August 2011 07:41:16 PM +0100.