Hi Everyone!
Can anyone tell me how i can place a HYPERLINK on a FORM and activate the link when clicked using the mouse. Suggestions and comments with examples will greatly be appreciated.
Thanks in advance.
Dilson
[email protected]
Printable View
Hi Everyone!
Can anyone tell me how i can place a HYPERLINK on a FORM and activate the link when clicked using the mouse. Suggestions and comments with examples will greatly be appreciated.
Thanks in advance.
Dilson
[email protected]
create a module with this code:
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
'
'
Public Sub OpenFile(Path As String)
Call ShellExecute(App.hInstance, "Open", _
Path, "", Path, 1)
End Sub
create a form with this code and a command button:
Option Explicit
Private Sub Command1_Click()
OpenFile "http://www.vbworld.com"
End Sub
instead of a command button you could use a label & mouse move to get color change and underline effects
If you want a more refined touch try using drag and drop instead of mouse move like this (just remember to set the labels DragIcon (properties window) to the desired cursor):
Private Sub Label1_DragDrop(Source As Control, x As Single, Y As Single)
If Source = Label1 Then
OpenFile "http://www.vbworld.com"
End If
End Sub
'
Private Sub Label1_DragOver(Source As Control, x As Single, Y As Single, State As Integer)
If State = vbLeave Then
With Label1
.Drag vbEndDrag
.ForeColor = vbBlack
End With
End If
End Sub
'
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
With Label1
.Drag vbBeginDrag
.ForeColor = vbBlue
.Font.Underline = True
End With
End Sub
This give a kind of mouse leave event
First, Create a label and call it "lblUrl"
Next, Have a module that contains the following code:
---------------------
Public Const URL = "http://www.Whatever.com"
Private Const SW_SHOWNORMAL = 1
Public Sub HyperLink()
Dim lSuccess As Long
lSuccess = ShellExecute(0&, vbNullString, URL, vbNullString, "C:\", SW_SHOWNORMAL)
If lSuccess <= 32 Then
MsgBox "Error", vbCritical
End If
End Sub
---------------------
Third Add this code to the form
---------------------
Private Sub lblURL_Click()
HyperLink
End Sub
Private Sub Form_Load()
lblurl.ForeColor = &HFF0000
End Sub
-------------------
The following code may also be added to give a "Visited" look:
-------------------
Private Sub lblurl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
lblurl.ForeColor = &HFF&
End Sub
-------------------
Bye
If you want something a little cleaner and easier to use, then you could checkout my Freeware YoungURL OCX
It includes a few extra features like:
Customizable..
Link Color/Hover Color
Hover Style, (Bold, Underline, Strikethrough & None)
Specify wether to use a New instance of a Browser/Email Client or an Existing one.
etc..
If you use it, let me know what you think.
I also wrote my own HyperLink OCX available at my website:
http://www.cromas.net/
Go to Visual Basic:Controls
Good Luck!