Start new project.
Main form size: 700, 380. Main form windowstate: Maximized.
Add a combobox on top of the main form, size around 580, 21. Combobox anchor: Top, Left, Right.
Add a button on the right of the combobox, text: Navigate, anchor: Top, Right.
Below the combobox and the button add a WebBrowser from the toolbox. Size around 660, 280
WebBrowser anchor: Top, Bottom, Left, Right
Add a timer to the program.
Double click the button, add this into the sub:
WebBrowser1.Navigate(ComboBox1.Text)
- start program. Insert a working web address like google.com, and press the button.
If you wanted to, you could have inserted the web address directly in the code, this is how: WebBrowser1.Navigate("www.google.com")
Go to the design page, click on the combobox, go to properties, click on events icon, double click on the KeyDown event. We want to be able to insert the web address into the combobox text and navigate to it by pressing the "enter" key. At this point, every time we press the "enter" key we hear a warning sound which is inconvenient.
Inside the combobox sub add:
If e.KeyCode = Keys.Enter And ComboBox1.Text.Length >= 0 Then
e.SuppressKeyPress = True
Button1_Click(Nothing, Nothing)
End If
- after the "enter" key is pressed, e.SuppressKeyPress will stop the notification sound that we don't want, also button1 will be called.
Now we will add the ability to double click the Navigate button and send the web address to our default windows web browser. Generally buttons can't use the double click event, so we will do this with a timer.
In the declaration area, outside button1 sub, insert this:
Dim t1 as Integer
Change the content of the button1 sub into this:
If Timer1.Enabled = False Then
Timer1.Start()
End If
t1 = t1 + 1
- the timer starts only if it's turned off. Every time you press the Navigate button, "t1" is raised by 1.
Go to the design page, double click the timer control, insert in the sub:
Timer1.Stop()
If t1 = 1 Then
WebBrowser1.Navigate(ComboBox1.Text)
ElseIf t1 > 1 Then
System.Diagnostics.Process.Start(ComboBox1.Text)
End If
t1 = 0
ComboBox1.Items.Add(ComboBox1.Text)
- the timer gets stopped directly. After the timer has been started (button1), the timer interval has to first end before the code from the timer sub is executed. During that interval a person should have enough time to decide if he wants to click on the button once or a couple of times.
If there is only one click, the visual basic webbrowser control will navigate to the web address.
System.Diagnostics.Process.Start(...) - if there are more clicks found, then the default OS web browser should visit the desired website.
ComboBox1.Items.Add(ComboBox1.Text) - addresses we have already navigated to will be added to the combobox. The problem with this is that if we navigate the same site twice, it will be added twice.
Dim t1 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Timer1.Enabled = False Then
Timer1.Start()
End If
t1 = t1 + 1
End Sub
Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter And ComboBox1.Text.Length >= 0 Then
e.SuppressKeyPress = True
Button1_Click(Nothing, Nothing)
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
If t1 = 1 Then
WebBrowser1.Navigate(ComboBox1.Text)
ElseIf t1 > 1 Then
System.Diagnostics.Process.Start(ComboBox1.Text)
End If
ComboBox1.Items.Add(ComboBox1.Text)
End Sub
End Class
If you are sending an invalid address name to your OS browser, the program will most likely crash. This must be fixed.
Change this line of code in the timer sub:
System.Diagnostics.Process.Start(ComboBox1.Text)
into this:
- now invalid web addresses will give you a google search instead of crashing.
Change this line:
- if the same address was already used in the combobox, then we will exit the sub and avoid adding it again.
Final code:
Dim t1 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Timer1.Enabled = False Then
Timer1.Start()
End If
t1 = t1 + 1
End Sub
Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter And ComboBox1.Text.Length >= 0 Then
e.SuppressKeyPress = True
Button1_Click(Nothing, Nothing)
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
If t1 = 1 Then
WebBrowser1.Navigate(ComboBox1.Text)
ElseIf t1 > 1 Then
Try
t1 = 0
For Each i In ComboBox1.Items
End Class
Start program.
You could also insert four more buttons to this program if you prefer. Code for the first one:
WebBrowser1.GoBack()
- revisit previously used web addresses
A button for moving forward if you ever navigated back:
WebBrowser1.GoForward()
A button for refreshing the current website:
WebBrowser1.Refresh()
A button that cancels the loading of a website:
WebBrowser1.Stop()