creating clickable hyperlinks

Posted on 2000-02-21
Last Modified: 2012-06-27
how can I create clickable hyperlinks on a textbox/richtextbox using VB.

Thanks in advance.
Question by:taimur
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Accepted Solution

mcrider earned 50 total points
ID: 2542102
LVL 70

Expert Comment

by:Éric Moreau
ID: 2542107
Save it as a control:

Begin VB.UserControl XLinkLabel
   CanGetFocus     =   0   'False
   ClientHeight    =   390
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   420
   MouseIcon       =   "LinkLabel.ctx":0000
   MousePointer    =   99  'Custom
   ScaleHeight     =   390
   ScaleWidth      =   420
Attribute VB_Name = "XLinkLabel"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

' XLinkLabel
' Author: David Crowell (
' See for updates
' Released to the public domain
' Last update: May 22, 1999
' Use this code at your own risk.  I assume no liability for
' the use of this code.
' The purpose of XLinkLabel is to have a label control
' that works as a hyperlink.

' API Declares
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
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
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

' Module level variables
Private nControlHeight As Long
Private nControlWidth As Long
Private bHovering As Boolean

' Property member variables
Private mBackColor As OLE_COLOR
Private mNormTextColor As OLE_COLOR
Private mHoverTextColor As OLE_COLOR
Private mNormUnderline As Boolean
Private mHoverUnderline As Boolean
Private mFont As StdFont
Private mCaption As String
Private mURL As String
Private mEnabled As Boolean

' The property Get/Let/Set stuff if pretty
' self-explanatory, so figure it out :)

Public Property Get BackColor() As OLE_COLOR
Attribute BackColor.VB_ProcData.VB_Invoke_Property = ";Appearance"
Attribute BackColor.VB_UserMemId = -501
    BackColor = mBackColor
End Property
Public Property Let BackColor(NewColor As OLE_COLOR)
    mBackColor = NewColor
    UserControl.BackColor = mBackColor
    PropertyChanged "BackColor"
End Property

Public Property Get NormTextColor() As OLE_COLOR
Attribute NormTextColor.VB_ProcData.VB_Invoke_Property = ";Appearance"
Attribute NormTextColor.VB_UserMemId = -513
    NormTextColor = mNormTextColor
End Property
Public Property Let NormTextColor(NewColor As OLE_COLOR)
    mNormTextColor = NewColor
    UserControl.ForeColor = NewColor
    PropertyChanged "NormTextColor"
End Property

Public Property Get HoverTextColor() As OLE_COLOR
Attribute HoverTextColor.VB_ProcData.VB_Invoke_Property = ";Appearance"
    HoverTextColor = mHoverTextColor
End Property
Public Property Let HoverTextColor(NewColor As OLE_COLOR)
    mHoverTextColor = NewColor
    PropertyChanged "HoverTextColor"
End Property

Public Property Get NormUnderline() As Boolean
    NormUnderline = mNormUnderline
End Property
Public Property Let NormUnderline(val As Boolean)
    mNormUnderline = val
    UserControl.FontUnderline = val
    PropertyChanged "NormUnderline"
End Property

Public Property Get HoverUnderline() As Boolean
Attribute HoverUnderline.VB_ProcData.VB_Invoke_Property = ";Appearance"
    HoverUnderline = mHoverUnderline
End Property
Public Property Let HoverUnderline(val As Boolean)
    mHoverUnderline = val
    PropertyChanged "HoverUnderline"
End Property

Public Property Get Font() As StdFont
Attribute Font.VB_ProcData.VB_Invoke_Property = ";Appearance"
Attribute Font.VB_UserMemId = -512
    Set Font = mFont
End Property
Public Property Set Font(NewFont As StdFont)
    Set mFont = NewFont
    Set UserControl.Font = mFont
    PropertyChanged "Font"
End Property

Public Property Get Caption() As String
Attribute Caption.VB_ProcData.VB_Invoke_Property = ";Appearance"
Attribute Caption.VB_UserMemId = -518
    Caption = mCaption
End Property
Public Property Let Caption(val As String)
    mCaption = val
    PropertyChanged "Caption"
End Property

Public Property Get URL() As String
Attribute URL.VB_ProcData.VB_Invoke_Property = ";Behavior"
    URL = mURL
End Property
Public Property Let URL(val As String)
    mURL = val
    PropertyChanged "URL"
End Property

Public Property Get Enabled() As Boolean
Attribute Enabled.VB_UserMemId = -514
    Enabled = mEnabled
End Property
Public Property Let Enabled(val As Boolean)
    mEnabled = val
    UserControl.Enabled = val
    PropertyChanged "Enabled"
End Property

' set up the default values
Private Sub UserControl_InitProperties()
    Set Font = Ambient.Font
    BackColor = Ambient.BackColor
    NormTextColor = Ambient.ForeColor
    HoverTextColor = Ambient.ForeColor
    URL = "" ' got to put my plug in here :)
    Enabled = True
    Caption = UserControl.Extender.Name
    NormUnderline = False
    HoverUnderline = True
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
' Read saved properties
    On Error Resume Next
    Set Font = PropBag.ReadProperty("Font", Ambient.Font)
    BackColor = PropBag.ReadProperty("BackColor", Ambient.BackColor)
    NormTextColor = PropBag.ReadProperty("NormTextColor", Ambient.ForeColor)
    HoverTextColor = PropBag.ReadProperty("HoverTextColor", Ambient.ForeColor)
    URL = PropBag.ReadProperty("URL", "")
    Enabled = PropBag.ReadProperty("Enabled", True)
    Caption = PropBag.ReadProperty("Caption", "URL Label")
    NormUnderline = PropBag.ReadProperty("NormUnderline", False)
    HoverUnderline = PropBag.ReadProperty("HoverUnderline", True)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
' Write properties (used only during design time)
    PropBag.WriteProperty "Font", Font
    PropBag.WriteProperty "BackColor", BackColor, Ambient.BackColor
    PropBag.WriteProperty "NormTextColor", NormTextColor, Ambient.ForeColor
    PropBag.WriteProperty "HoverTextColor", HoverTextColor, Ambient.ForeColor
    PropBag.WriteProperty "URL", URL, ""
    PropBag.WriteProperty "Enabled", Enabled, True
    PropBag.WriteProperty "Caption", Caption, "URL Label"
    PropBag.WriteProperty "NormUnderline", NormUnderline, False
    PropBag.WriteProperty "HoverUnderline", HoverUnderline, True
End Sub

Private Sub UserControl_Resize()

    ' Store the size of the usercontrol for faster use later
    nControlHeight = UserControl.Height
    nControlWidth = UserControl.Width
End Sub

Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim bHover As Boolean
    ' Always call release capture
    Call ReleaseCapture
    ' Is the mouse over the control?
    If (x < 0) Or (y < 0) Or (x > nControlWidth) Or (y > nControlHeight) Then
        bHover = False
        bHover = True
        ' if so be sure to call SetCapture, so we'll catch it when it moves off
        Call SetCapture(UserControl.hWnd)
    End If
    If bHovering <> bHover Then
    ' only change appearance if necessary
        bHovering = bHover
        ' we change the font setting of the usercontrol
        ' which will take effect next time we repaint
        If bHovering Then
            UserControl.FontUnderline = mHoverUnderline
            UserControl.ForeColor = mHoverTextColor
            UserControl.FontUnderline = mNormUnderline
            UserControl.ForeColor = mNormTextColor
        End If
        ' repaint
    End If
End Sub

Private Sub UserControl_Paint()

    ' erase current contents
    ' and repaint the text
    Call TextOut(UserControl.hDC, 0, 0, mCaption, Len(mCaption))
End Sub

Private Sub UserControl_Click()
' Open the default browser with the URL

    Call ShellExecute(0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus)
End Sub


Expert Comment

ID: 2543984
McRider Have you actually got that one to work? I try tyoing in an address but nothin happens. But then agin, thats me.

Do you have any sugg on my OLE probbs?

Author Comment

ID: 2544961
LVL 14

Expert Comment

ID: 2545188
You're welcome! Glad I could help!


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question