Important alert: (current site time 5/21/2013 6:28:43 PM EDT)
 

VB icon

RichTextBox Hyperlinks

Email
Submitted on: 9/14/2012 11:05:39 AM
By: J.A. Coutts 
Level: Intermediate
User Rating: By 2 Users
Compatibility: VB 6.0
Views: 2664
 
     Hyperlinks created using system calls have a few advantages such as detecting the link as you type, but if all you need is to identify links and respond to them, it can be done simply with VB commands only. Update: It turns out the original code worked only because there was one link in the sample. The SelStart property only works on the Text portion of the RichTextBox. It does not work correctly with the TextRTF portion. Web links are not supposed to contain any spaces, so I searched for the preceding space character. Then I searched for the ending Chr$(127) which I added to each link because it is a non-printable character. Now it works with multiple links.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: RichTextBox Hyperlinks
' Description:Hyperlinks created using system calls have a few advantages such as detecting the link as you type, but if all you need is to identify links and respond to them, it can be done simply with VB commands only.
Update: It turns out the original code worked only because there was one link in the sample. The SelStart property only works on the Text portion of the RichTextBox. It does not work correctly with the TextRTF portion. Web links are not supposed to contain any spaces, so I searched for the preceding space character. Then I searched for the ending Chr$(127) which I added to each link because it is a non-printable character. Now it works with multiple links.
' By: J.A. Coutts
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=74547&lngWId=1'for details.'**************************************

-Start a new project and add the Microsoft Rich Textbox Control component
-Drop a RichTextBox control and a Command Button control onto the form
-Expand the RichTextBox to at least 80 characters wide and enable Verical Scrollbars
-Rename the controls appropriately, and add the code below
-Run the program and copy some text with "http://" links in it to the RichTextBox
-Click the cmdFind button and links will be made blue and underlined
-Double Click a link, and the page will be loaded in your default Web browser
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Sub FindLinks()
Dim M%, N%
Dim strTemp1 As String
With RTB1
N% = .Find("http")
Do Until N% < 0
M% = .Find(vbCrLf, N%)
If M% = 0 Then M% = Len(.Text)
.SelStart = N%
.SelLength = M% - N%
strTemp1 = " \cf1\ul " & Mid$(.Text, N% + 1, M% - N%) & Chr$(127) & "\cf0\ulnone "
Debug.Print strTemp1
.SelRTF = strTemp1
N% = .Find("http", M%)
Loop
.Text = Replace(.Text, vbCrLf, "\par ")
End With
End Sub
Sub AddFormat()
RTB1 = "{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courrier New;}}" & Chr(13) _
& "{\colortbl ;\red0\green0\blue255;} " & Chr(13) _
& "\viewkind4\uc1\pard\lang1033\f0\fs19 " _
& RTB1.Text & Chr(13) & "\par }"
End Sub
Private Sub cmdFind_Click()
Call FindLinks
Call AddFormat
End Sub
Private Sub RTB1_DblClick()
Dim RetVal As Long
Dim lStart As Long
Dim lEnd As Long
Dim HyperLink As String
With RTB1
If .SelColor = vbBlue And .SelUnderline Then
'Step backwards to the beginning of the Hyperlink
.UpTo " ", False
lStart = .SelStart + 1
If lStart > 1 Then
'Step forward to the end of the hyperlink
.UpTo Chr$(127), True
lEnd = .SelStart + 1
If lEnd > lStart Then
HyperLink = Mid$(.Text, lStart, lEnd - lStart)
Else
MsgBox "Could not find end of link!", vbExclamation
Exit Sub
End If
Else
MsgBox "Could not find start of link!", vbExclamation
Exit Sub
End If
'Now launch the default browser to that page
RetVal = ShellExecute(0&, "open", HyperLink, 0, 0, 1)
End If
End With
End Sub


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/13/2012 2:31:19 AMBonnie West

Bugs in the code:

1. ShellExecute was unused, thus nothing happens when link is clicked
2. Repeatedly clicking cmdFind appended Chr(127) multiple times
3. Cursor didn't change while over the links
4. Even malformed URLs got highlighted
5. Valid URLs like www or mailto are unsupported
(If this comment was disrespectful, please report it.)

 
9/13/2012 11:09:23 AMHans J. Reich

ShellExecuteA is not called anywhere in the code provided, so something is missing

The link to what might be more code:

http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.74547/lngWId.1/qx/vb/scr ipts/ShowCode.htm

Does
not work
(If this comment was disrespectful, please report it.)

 
9/14/2012 12:17:22 PMJ.A. Coutts

My apologies. All the code did not go through the first time. This time, I have verified it is all there. This is demonstration code only. It is not intended as stand alone code. It does not have all the features of more complex code that uses system calls, but it can be used to hyperlink any text, not just URLs. If you intend to allow text that includes spaces to be hyperlinked, you must add code to that effect.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.