Click here to Skip to main content
13,633,280 members
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 17:31
memberjp73129-Oct-07 17:31 
GeneralRe: Google Parser online tool (GooParser) Pin
nasima jumma20-Jan-09 20:31
membernasima jumma20-Jan-09 20:31 
GeneralDon't work :( Pin
yarns16-Nov-05 3:13
memberyarns16-Nov-05 3:13 
GeneralRe: Don't work :( Pin
overture10-Nov-06 5:04
memberoverture10-Nov-06 5:04 
GeneralGlade Connection Problem Pin
Kikketer21-Jul-05 16:47
memberKikketer21-Jul-05 16:47 
GeneralRe: Glade Connection Problem Pin
Anonymous25-Aug-05 8:33
sussAnonymous25-Aug-05 8:33 
GeneralRe: Glade Connection Problem Pin
Kikketer5-Sep-05 5:31
memberKikketer5-Sep-05 5:31 
QuestionColor of text on Button? Pin
Priyank Bolia5-Mar-05 22:14
memberPriyank Bolia5-Mar-05 22:14 
AnswerRe: Color of text on Button? Pin
samirkut6-Mar-05 13:49
membersamirkut6-Mar-05 13:49 
GeneralRe: Color of text on Button? Pin
Priyank Bolia6-Mar-05 18:10
memberPriyank Bolia6-Mar-05 18:10 
QuestionChange Button Text at Runtime? Pin
Priyank Bolia5-Mar-05 10:22
memberPriyank Bolia5-Mar-05 10:22 
AnswerRe: Change Button Text at Runtime? Pin
Priyank Bolia5-Mar-05 20:12
memberPriyank Bolia5-Mar-05 20:12 
Questioniwhy not use java? Pin
Anonymous10-Feb-05 11:24
sussAnonymous10-Feb-05 11:24 
AnswerRe: iwhy not use java? Pin
Victor HQ10-Feb-05 23:11
memberVictor HQ10-Feb-05 23:11 
GeneralRe: iwhy not use java? Pin
LordFlies19-Mar-05 3:56
memberLordFlies19-Mar-05 3:56 
GeneralRe: iwhy not use java? Pin
Victor Vatamanescu-Hanga19-Mar-05 22:27
memberVictor Vatamanescu-Hanga19-Mar-05 22:27 
Yes, I do know SWT. In fact I was one of those who seriously considered to join the porting of SWT on mono in order to write the mono IDE, monodevelop, on top of it.
Hmmm... why not use SWT... Why not Tcl/Tk or PyGtk?!
I personally do some development in mono in my personal very little spear time. I got used to sleep less to do some free coding. There are sacrifices we as open source developers are making and it really hurts when fellow developers, instead of really testing and coming with bugs or fixing our errors, come with marketing slogans, "Java is mature and well proven". Thats stupid and 0% computer science. Perl is much more mature. So why don't you do your development in Perl, or better yet C?
I actually believe in mono. It is the best opportunity open source ever had to come closer to Microsoft's application portfolio through a truly easy and evolutive migration path.It is not because I believe that Microsoft has the best architectural thinking of all times.
GeneralRe: iwhy not use java? Pin
Anonymous19-Oct-05 9:25
sussAnonymous19-Oct-05 9:25 
Generalimages missing Pin
uTILLIty7-Feb-05 18:45
memberuTILLIty7-Feb-05 18:45 

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 | Cookies | Terms of Use | Mobile
Web01 | 2.8.180712.1 | Last Updated 14 Jan 2005
Article Copyright 2005 by samirkut
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid