Page 1 of 1

VB.NET Browser "Unibar" Create a search bar that allows web browser browsing and search engine Rate Topic: -----

#1 mrmcpott  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 95
  • Joined: 24-November 08

Posted 28 December 2009 - 12:05 AM

This will explain one method how to create a "unibar" type search address bar. This is similar to Google Chrome's address bar.
NOTE: This code will only work for the Google Search engine, but can easily be optimized.
This code assumes the following:

You have a form named "frmMain"
You have a WebBrowser Control named "webBrowser"
You have a ToolStrip with a label named "tslSearch", a combobox named "cboSearch" and a button named "btnGo"
The default text values are respectively: "Browse:", "", "&Go"

Now, the idea behind it: This user is able to type in any URL, IP Address, etc into the search bar. However, if the user enters in something along the lines of "" or "" and hits the [Space] key, the "SearchMode" is activated.

Now for the coding:


We are going to declare a boolean variable "blnSearchMode" and initialize it to false:
Public Class frmMain
	Dim blnSearchMode As Boolean = False

End Class

Now, we are going to code the combobox "cboSearch":
   Private Sub cboSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboSearch.KeyDown
		' If the user presses space, check to see if they are going to search Google.
		If e.KeyCode = Keys.Space Then
			If cboSearch.Text.Contains("") Then
				' If the user entered in "", then enter search mode.
				blnSearchMode = True
				' Set up the interface to show this:
				tslSearch.Text = "Search Google:"
				cboSearch.Text = ""
				blnSearchMode = False
				tslSearch.Text = "Browse:"
			End If
		End If
	End Sub

The above code will detect if the user wants to searh Google. If so, the interface is optimized for searching Google.

Now, the "btnGo" code:

Idea: If the browser is in search mode, we will use the text in cboSearch to search Google, if not, simply pass the cboSearch text as a URL.
 If blnSearchMode = True Then
			' If the browser is in search mode, search Google.
			webBrowser.Navigate("" & cboSearch.Text & "&aq=f&oq=&aqi=g10")
			' If the browser is not in search mode, browse as normal.
		End If
	End Sub

Now, let's say the user wants to exit "search mode." This is covered below.

Idea: the user deletes all the text from cboSearch. And hits backspace twice to revert to browse mode.

Create an integer named intBackSpace below blnSearchMode

	Dim blnSearchMode As Boolean = False
	Dim intBackSpace As Integer = 0

Now, in the cboSearch KeyDown event, add the following:

If e.KeyCode = Keys.Back Then
			If cboSearch.Text = "" Then
				intBackSpace += 1
				If intBackSpace = 2 Then
					blnSearchMode = False
					tslSearch.Text = "Browse:"
					intBackSpace = 0
				End If
			End If
		End If

This code works 100%! Developed it as i wrote this tutorial, so I know it works. Hope its useful!

Is This A Good Question/Topic? -1
  • +

Page 1 of 1