Click here to Skip to main content
12,821,041 members (37,873 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 14 Jan 2005

A google search application using Gtk#

, 14 Jan 2005
Rate this:
Please Sign up or sign in to vote.
An application (based on Gtk#) which uses the google web service to search the internet.

Sample Image - googleSearch.jpg


GTK (GIMP Toolkit) is a library for creating graphical user interfaces. It is licensed using the LGPL license, so you can develop open software, free software, or even commercial non-free software using GTK without having to spend anything for licenses or royalties. Gtk# is a binding for mono to this great toolkit, that is usable from any Mono language including MonoBasic and C#. There is also a binding for .NET Framework v1.1 on Windows.

Gtk can be installed for both Windows and Linux, though the Linux version is a little faster and more stable as compared to the Windows version. I am going to show how to create a cross-platform application using .NET and Gtk#. The development environment I describe here is based on windows. However you can also use Linux as your development environment.

The application will perform an internet search using the Google webservice. Firstly, you need to have a google developer account. If you don’t have an account you can create one at

Once your account is created you’ll receive a licence key from google which is 32 byte alphanumeric string e.g. 0AdaQ5lQFHJmWhI5UACEjtgIcwRezTJ5. Keep this safe since this is required for every web service call you make to Google web service.

Development Environment (Windows)

The development environment on windows for Gtk# apps consists of the following:

  1. Visual Studio .NET 2003 (IDE)
  2. Gtk# (cross platform UI toolkit)
  3. Glade (UI designer)

(If you don’t have Visual Studio you can also use SharpDevelop. You can either use .NET Framework v1.1 for development or configure your Visual Studio to use Mono.)

The executables produced can be run directly on Linux workstations with Mono and Gtk# installed. They can also be run on any windows workstation which has .NET Framework 1.1 (or Mono) and Gtk# installed.

Note: You need to install Glade only on the development machines.

Setting up the development environment (Windows)

  1. Installing Gtk#

    The Gtk# homepage contains a link to a win32 installer. Download and install. There are 2 versions available on the website – v1.0.2.4 and v1.9.1.0. I have tried out v1.0.2.4 and it works perfectly though the latter version should also work.
  2. Installing Glade

    Download and install Glade from You also have to download and install the Gtk+ runtime from the same website.

Development Environment (Linux)

The development environment on windows for Gtk# apps consists of the following:

  1. MonoDevelop (IDE)
  2. Gtk# (cross platform UI toolkit)
  3. Glade (UI designer)

The executables produced can be run directly on Linux workstations with Mono and Gtk# installed. They can also be run on any windows workstation which has .NET Framework 1.1 (or Mono) and Gtk# installed.

Note: You need to install Glade only on the development machines.

Setting up the development environment (Linux)

  1. Installing MonoDevelop

    Refer to its website for installation instructions.
  2. Installing Gtk#

    The instructions can vary depending on what version of Linux you run. Refer to for instructions.
  3. Installing Glade

    Refer to for latest version and installation instructions.


Now that you have your development platform setup let me explain the various components and how they interact with each other. Glade is a click-n-drag UI designer. It is used to create the user interface for your application. Once the user interface is complete it saves the UI as an XML file. This xml file is used by the .NET(or Mono) application to build user interface at runtime. The application uses the Gtk# and Glade# library (Glade# is packaged with the Gtk#) to read the XML and create a UI.

Building the interface

The first step is to build the user interface. Open Glade. If you can’t find it on your desktop, it should be present in C:\Program Files\Gtk\bin.

You should see 3 windows open up.


Click on the window icon in the Palette window. A window is created.

Glade window

Change the window properties using the Properties dialog. Set Window name to mainWindow and Title to “Gtk# - Google Search”. You should also set the default width and height.

Glade Properties

Next click the vertical box button ( vertical) and click in the window. Select 2 rows when prompted.


Your window will now be divided into 2 parts.


To make space to add a textbox and buttons, click on the horizontal box (Horizontal ) and click on the top half of the vertical box you created earlier. Choose 3 columns when prompted.

Sample screenshot

In the three columns of the top half of the window, add a text entry and 2 buttons. After you do so your interface should like this.

Sample screenshot

Now right click on the top half of the window and choose hbox->select. Change its Packing property so that Expand and Fill are “No”.

Sample screenshot

Click on button1 in the window and change its Name property to btnSearch, Label to Search and Icon property to Find.

Sample screenshot

Click on button2 in the window and change its Name property to btnClear, Label to Clear and Icon property to Clear.

Sample screenshot

Now your window should look like this.

Sample screenshot

Now all you need to do is to add in a search results box. Add a text view component to the lower half of the window. At this point the interface should look like this.

Sample screenshot

Now save the interface

Sample screenshot

In the save options, you can ignore the “C Options” tab and the “LibGlade Options” tab. That should complete your UI design. Now you’ll go on to linking up the code with the user interface.


Fire up your visual studio 2003 and create a new empty C# project. Call it googleSearch.

Add Reference to the following assemblies: atk-sharp, gtk-sharp, gdk-sharp, glade-sharp, glib-sharp. In the project properties set the Output Type to “Windows Application”

Add the to the project. Right-click on the filename in the solution explorer and change the Build Action property to “Embedded Resource”. This ensures that the file is embedded inside the executable produced by compiling the project.

Lets add a web reference to the Google Web Service. Right click on References in the solution explorer and choose to add a new web reference. Type the url and press Go. You should see the following screen.

Adding web sreference in Visual Studio

Press Add Reference. This creates the classes necessary to connect to the google web service. (If you are not using Visual Studio, you can do the same by using the wsdl.exe command line program which is a part of the .NET Framework SDK and can be found at C:\Program Files\Microsoft.NET\SDK\v1.1\Bin.)

Add a new class called GtkApp.cs.

using System;
using Gtk;
using Glade;

namespace googleSearch
 /// <summary>
 /// Summary description for GtkApp.
 /// </summary>
 public class GtkApp
  [Widget]Window mainWindow;
  [Widget]Button btnSearch;
  [Widget]Button btnClear;
  [Widget]Entry entry1;
  [Widget]TextView textview1;

  /* Main Function */
  public static void Main (string[] args)
   new GtkApp (args);

  public GtkApp(string []args)

   Glade.XML gxml = new Glade.XML (null, "", 
                                  "mainWindow", null);
   gxml.Autoconnect (this);
   mainWindow.DeleteEvent+=new DeleteEventHandler(mainWindow_DeleteEvent);
   btnSearch.Clicked+=new EventHandler(btnSearch_Clicked);
   btnClear.Clicked+=new EventHandler(btnClear_Clicked);

  private void mainWindow_DeleteEvent(object o, DeleteEventArgs args)
   Application.Quit ();
   args.RetVal = true;

  private void btnSearch_Clicked(object sender, EventArgs e)

    //do search
    GoogleSearchService svc=new GoogleSearchService();
    GoogleSearchResult result
    foreach(ResultElement re in result.resultElements)
     textview1.Buffer.Text+="Title: "+re.title+"\r\n";
     textview1.Buffer.Text+="Summary: "+re.summary+"\r\n";
     textview1.Buffer.Text+="URL: "+re.URL+"\r\n";

   catch(Exception ex)
    Gtk.MessageDialog md=new MessageDialog(mainWindow,Gtk.DialogFlags.Modal,

  private void btnClear_Clicked(object sender, EventArgs e)


Now build and start the project. You should see a window that looks like the following.

Sample screenshot

Type some terms in the text box and press search. You’ll see search results, which are returned by Google.

I’ll go through some of the more important parts of the code now.

[Widget]Window mainWindow;

This line defines mainWindow to be of type Gtk.Window. The name must match the name defined in Glade. The same holds true for the remaining declarations.


This line initializes the Gtk Application.

Glade.XML gxml = new Glade.XML (null, "", 
                               "mainWindow", null);
gxml.Autoconnect (this);

This loads the XML file and defines mainWindow to be the root element. The Autoconnect call links up widget names with the declarations made previously. E.g. The variable mainWindow defined in the class declaration is linked to the mainWindow defined in the file.

mainWindow.DeleteEvent+=new DeleteEventHandler(mainWindow_DeleteEvent);
btnSearch.Clicked+=new EventHandler(btnSearch_Clicked);
btnClear.Clicked+=new EventHandler(btnClear_Clicked);

These lines define the event handlers for various events. It is not necessary to define the events manually. You can actually define the event handlers using Glade. In that case, when Autoconnect call is made it’ll automatically link up the events to the event handlers.


This starts the Gtk Application.

GoogleSearchResult result
      = svc.doGoogleSearch("FEkTsl8kwgQtrQIeGHJDSELrTtjNcp1eC",

This makes a web service call to the Google web service. The result contains the search results. The first parameter is your License key which you should have obtained from Google as described earlier in this article.

To run this program in Linux, simply recompile the project as Release, then copy the googleSearch.exe file found in the bin\Release folder to the Linux machine. On the shell prompt type

# mono googleSearch.exe

This should start the program. Of course, you must have mono and gtk-sharp installed on the Linux machine.

Some tips for cross platform development

  1. There are some Namespaces that should be avoided as far as possible because they are not completely supported by Mono as yet.

  2. Use relative paths when naming files. The file C:\Program Files\yourapplication does not exist on Linux, and /usr/lib/ isn't available under Windows. Also remember that Linux filenames are case-sensitive unlike windows.
  3. Avoid platform-specific resources like the Windows registry (Microsft.Win32.Registry).
  4. Use Gtk# for building applications. Gtk# for Windows is better than System.Windows.Forms for Linux.
  5. Only use Platform Invoke (P/Invoke) if you really need it. If possible, use a library that is available on UNIX and Windows.
  6. Mono does not support incremental debug builds, which are used extensively by Visual Studio for faster compilation. When deploying files created in Visual Studio to Linux do a Rebuild as Release.
  7. Avoid using Visual Basic .NET since support for it in Mono is still beta.


This hopefully introduces you to the basics of how to use Gtk# in .NET. For more details, API listings etc take a look at the references section. The Gtk# API is very close to the Gtk API since its mainly a wrapper around Gtk. Hence, you can use the Gtk API itself as a reference. For more tutorials on using Gtk# refer to the Mono Handbook.


  1. Mono Handbook
  2. MonoWiki
  3. Glade# for Rapid Development
  4. Glade Tutorial
  5. GTK Tutorial


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Singapore Singapore
Samir Kuthiala founded a Singapore based technology firm Innove Technologies Pte Ltd in 2002. Innove Technologies specialises in providing enterprise level .NET solutions to their clients.

You may also be interested in...

Comments and Discussions

GeneralGoogle Parser online tool (GooParser) Pin
jp73129-Oct-07 18:31
memberjp73129-Oct-07 18:31 
I found this interesting Google Parser online tool named GooParser. It parses Google search results, you can use it online and get unlimited Google parsed search results, not only the first 10.

In this site there are another online tools, based on parsing Google search results, very interesting and useful for webmasters, you can look at Online Webmaster Tools.

GeneralRe: Google Parser online tool (GooParser) Pin
nasima jumma20-Jan-09 21:31
membernasima jumma20-Jan-09 21:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 14 Jan 2005
Article Copyright 2005 by samirkut
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid