Problem:
Create a program in vb6 that will allow the user to enter the address of the website in a textbox and open it in the default browser.
Solution:
We will be using the ShellExecute Windows API (Application Programming Interface). ShellExecute uses the shell to open or print a file or run a program.
Let’s start the tutorial
- Kindly open your visual basic 6 program and select Standard EXE.
- In this program we need the following controls:
- 1 label
- 1 textbox
- 2 command buttons
- The design of the form will be
- Next is right click the form and select View Code.
- Then paste the code below.
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
Code Explanation:
This is the ShellExecute API that we’re talking about.
- Next is we will create a function that will open the site. Paste the code below.
Public Function OpenLocation(url As String, WindowState As Long) As Long
Dim lhWnd As Long
Dim lAns As Long
' Execute the url
lAns = ShellExecute(lhWnd, "open", url, vbNullString, vbNullString, WindowState)
OpenLocation = lAns
End Function
- Double click the Open button and paste the code.
Dim i As Long
Dim urls As String
Dim indexpath As String
indexpath = "http://" & text1.Text & ""
urls = Trim(indexpath)
i = OpenLocation(urls, 1)
- To close the form, double click the close button and enter the code
Unload me
- Save the project and press F5 to run the project.