1. Home
  2. Computing & Technology
  3. Visual Basic

Writing Your Own Google API Program
The Code ...

By , About.com Guide

Although the code below is "scrunched up" against the left margin, if you copy and paste it into the VB .NET editor in Visual Studio .NET, it will be reformatted correctly.
Public Class GBMain
Inherits System.Windows.Forms.Form
Friend GSR As _
New com.google.api.GoogleSearchResult

#Region " Windows Form Designer generated code "

Private Sub GBText_DoubleClick( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles GBText.DoubleClick
' New Google Search Service and Result Element objects
Dim GSS As New com.google.api.GoogleSearchService
Dim GRE As New com.google.api.ResultElement
' GBResult is the second form
Dim GBResultForm As New GBResult
GBResultForm.thisForm = Me
GSR = _
GSS.doGoogleSearch("YourGoogleKeyGoesHere", _
GBText.SelectedText, _
0, CInt(GB_ResultsRequest.Text), _
False, "", False, "", "", "")
Dim estResultsNum As Integer = _
GSR.estimatedTotalResultsCount
GBResultForm.thisForm = Me
Try
' No results?
If GSR.resultElements Is Nothing Then
GBResultForm.GBRTitle.Text = _
"No Search Results Found"
GBResultForm.GBRUrl.Text = "**E1"
ElseIf estResultsNum _
< CInt(GB_ResultsRequest.Text) Then
GBResultForm.GBRTitle.Text = _
"Fewer Google hits than requested were found"
GBResultForm.GBRUrl.Text = "**E2"
End If
GBResultForm.Show()
Catch ex As _
System.Web.Services.Protocols.SoapException
MsgBox(ex.Message)
End Try
End Sub
End Class

Although the goal is to demonstrate a real application using the Google web service, there's one other thing that might catch your eye in this code and that's the use of a property in the GBResult form to pass the Google search result object. Here's how the main form works.

Initially, the program creates objects for use later. In the GBResult form object, the thisForm property is assigned to the main form. Since the Google objects are part of the main form, this will make it possible to reference them within the main form in the code in the GBResult form later. The code then does a standard request from the Google web service using the selected text and the requested number of results from form text boxs. One usage note: As it's currently written, you have to use the shift key to select a phrase and discontinuous words can't be selected.

The main call to the Google web service is enclosed in a Try block to catch any errors that happen there and some very basic error checking is done with the returned results. If result errors are found, they're passed to the GBResult form in the GBRUrl text box. Not the cleanest way to do it, but it's quick for this example. When all is ready, the GBResult form is displayed with the Show method.
The Result Form Code
Public Class GBResult
Inherits System.Windows.Forms.Form

Private MainForm As GBMain
Private HitCount As Integer = 0

#Region " Windows Form Designer generated code "

Property thisForm() As GBMain
Get
End Get
Set(ByVal Value As GBMain)
MainForm = Value
End Set
End Property

Private Sub GBRNext_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles GBRNext.Click
HitCount += 1
If _
HitCount _
< CInt(MainForm.GB_ResultsRequest.Text) Then
DisplayForm()
Else
GBRTitle.Text = _
"!!Hit Request Exceeded " & GBRTitle.Text
End If
End Sub

Private Sub GBResult_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
DisplayForm()
End Sub

Private Sub DisplayForm()
Dim DisplayFlag As String
DisplayFlag = _
Microsoft.VisualBasic.Left(GBRUrl.Text, 2)
If DisplayFlag <>> "**" Then
GBRCount.Text = _
"Displaying Hit " & _
CStr(HitCount + 1) & _
" of " & _
MainForm.GB_ResultsRequest.Text
GBRTitle.Text = _
MainForm.GSR.resultElements(HitCount).title
GBRUrl.Text = _
MainForm.GSR.resultElements(HitCount).URL
GBRSnip.Text = _
MainForm.GSR.resultElements(HitCount).snippet
End If
End Sub

Private Sub GBRExit_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles GBRExit.Click
Close()
End Sub

End Class

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Applications
  5. Writing Your Own Google API Program

©2009 About.com, a part of The New York Times Company.

All rights reserved.