Create a hyperlink

Most programs now include a hyperlink in their about box linking to their web site or product support email. That is simple enough, but some also include a 'rollover' effect, which is not quite so easy in VB. Fortunately, you can hack around this limitation by convincing VB that you are actually dragging something over the hyperlink... Then VB will give you the DragOver information you need. Below is some example source code. Create a label, and name it lblURL, and set its caption property to the internet address you want. If the URL is an email address, then un-comment the line in green, and comment out the line below that.

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 Sub lblURL_DragDrop(Source As Control, X As Single, Y As Single)
    If Source Is lblUrl Then
        With lblUrl
            .Font.Underline = False
            .ForeColor = vbBlack
            ' Call ShellExecute(0&, vbNullString, "Mailto:" & .Caption, vbNullString, vbNullString, vbNormalFocus)
            Call ShellExecute(0&, vbNullString, .Caption, vbNullString, vbNullString, vbNormalFocus)
        End With
    End If
End Sub

Private Sub lblURL_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
    If State = vbLeave Then
        With lblUrl
            .Drag vbEndDrag
            .Font.Underline = False
            .ForeColor = vbBlack
        End With
    End If
End Sub

Private Sub lblURL_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    With lblUrl
        .ForeColor = vbBlue
        .Font.Underline = True
        .Drag vbBeginDrag
    End With
End Sub

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow