Click here to Skip to main content
14,538,686 members

Calc# - An introduction to Gtk#

Rate this:
2.55 (17 votes)
Please Sign up or sign in to vote.
2.55 (17 votes)
11 Mar 2005
An introduction to the open source community of Mono, Gtk# and Glade. Learn how to write cross-platform GUI software.

Sample Image - calcsharp.jpg


Mono allows developers to build Linux and cross-platform applications easily. Mono's .NET implementation is based on the ECMA standards for C#. GTK+ is a multi-platform toolkit for creating graphical user interfaces. Offering a complete set of widgets, GTK+ is suitable for projects ranging from small one-off projects to complete application suites. Gtk# is a .NET language binding for the Gtk+ toolkit. Glade provides an easy interface to design the application GUI efficiently and fastly.

This article will teach you how to write cross platform GUI application using Gtk# and mono, and how to create the GUI interface using glade. Designing GUI with Glade is somewhat similar to designing forms in Visual studio. Although this article scope is limited to Windows 2000 or similar systems, the concepts can be similarly extended to Linux or other platforms.

Getting Started

To get started, first you need all the required software for building our application. The first and the foremost important thing is mono, which can be downloaded from the mono website. Just download the Windows installer and install it. I know you can do that yourself. This installer includes the GTK# package also. Now you have to run the setmonopath.bat file in the bin directory of your mono installation to setup up the environment variables. I don’t know why, but it never works with me, so I have to manually add that path in the environment variable.

Image 2

After installing mono, you need to install Glade. Download and install Glade from source forge website. You have to download and install the Gtk+/Win32 Development Environment. Now you are ready to start coding, but wait, we won’t be coding in notepad, so you need the Visual Studio .NET. This is the best editor in my choice and also more familiar to all of us. You can also use SharpDevelop which is freely available from the SharpDevelop website. But here, we’ll be using Visual Studio .NET. Now, we are all set to start the mission, 9 8 7 6 5 4 3 2 1 Starting Glade…


As a Windows developer you will be glad to know that designing GUI with glade is similar to designing dialog boxes in VC++ or Win Forms in .NET. Just run the “glade-2.exe” and you will see three windows at your screen. The first one is the main window, with a menu and a toolbar. It helps you to create new project, save and open current projects. Another one looks to have many controls icon on it with “Palette” caption. These controls are called widgets in Linux and mono. You will see some icons as horizontal box and vertical Box on that palette. This is one of the main differences between glade and Win Forms. These boxes are invisible and used for packaging other controls, it’s like a container. By packing widgets into boxes, you can create layouts that remain consistent at different window sizes, and orient boxes either vertically or horizontally.

Image 3

Now click on the window icon, in the palette bar. A new window appears on the screen. This is your application window GUI, that you have to modify. In the properties window, you can see the properties of that window. From the properties window, you can edit the caption and the variable name of the window to be used in your program. Change the “Type Hint” to “Menu” from the properties window, so that, maximize and minimize button does not appear in your application, when you run it. Add an event handler for the destroy event, which gets raised when the user clicks on the close button.

Image 4

Now add some vertical boxes and some horizontal boxes to those vertical boxes, except the top vertical box. Add an edit box to the top vertical box. The edit box is called Entry in GTK#. Add some buttons to each box. Set the padding to 5 and width to 35, height to 30. Also set the label accordingly. Add an event handler for button clicked and add the accelerator keys.

Image 5

Creating boxes for packaging widgets.

Image 6

Adding text entry and Fixed Positions to the GUI.

Image 7

Adding buttons and menu bar. Setting button's Label, Height, Width, Padding, Accelerators and Signal Handler for the first row Buttons.

Wait, Wait!, It would be useless to say click on that, and then click on that, as I had used some other widgets also, to make the GUI impressive like ToggleButton, Fixed Positions and Menu Bars. So, I would like you to open the "" file and see what I had done. We call it reverse engineering, the best training tool. After, you are finished with the GUI, just save the project file. For now on, just change the project name to calculator and safely ignore all other stuffs. This will create an XML file with ".glade" extension, which will contain all the information regarding the GUI and the variable names of the widgets by which we will access them in our code and all the event handlers and widgets property information. I would like you to refer to "A Google search application using GTK#" at CodeProject for further information in this regard.


Now the sets are all ready, but the shooting has not started yet. Where is our hero "Mono"? So let’s bring our hero to center stage. Mono is an open source C# compiler in which we will compile our program, which consumes the GUI interface in the XML file generated by the glade.

To use all the classes and power of GTK#, we will have to import its namespaces firstly. So, import all the namespaces:

using Gtk;
using Gdk;
using Glade;
using GtkSharp;
using System;
using System.Text;

After that we will declare a class Calculator and a static Main function in it, as the entry point of our program. In the Main function we create an object of our class Calculator. As Main is defined as static, it will get called automatically by the compiler.

In the constructor of our class, we will initialize GTK#, by calling Application.Init();. Now we create an object of Glade.XML which contains the GUI description. We will pass the name of our XML file and the top level widget to the Glade.XML constructor. The Autoconnect(this) method simply creates the controls and connects it to the corresponding event handler defined in that XML file. Run causes the flow of control to enter into the main event loop.

Now we will write the window delete event handler, by writing a function with name OnWindowDeleteEvent and with parameters as an object (reference to the sending object) and the DeleteEventArgs. The function name should be the same as defined in the XML file by glade. The delete event does two tasks, first it quits the main event loop by calling the Application.Quit(); and secondly it sets the args.RetVal to true, preventing any subsequent handlers from getting this event back.


C:\>mcs /unsafe –r gtk-sharp.dll –r gdk-sharp.dll –r glade-sharp.dll calculator.cs
C:\>mono calculator.exe


  • Mono – A Developer’s Notebook

    By Edd Dumbill and Niel M.Bornstein.


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

Priyank Bolia
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

QuestionThanks a lot for this article Pin
Steffen Ploetz2-Feb-15 8:15
professionalSteffen Ploetz2-Feb-15 8:15 
GeneralI builed it, but Pin
TomasLinhart11-Aug-06 4:32
MemberTomasLinhart11-Aug-06 4:32 
QuestionAccelerator Keys with Glade Pin
jggonzalez8-Oct-05 4:28
Memberjggonzalez8-Oct-05 4:28 
AnswerRe: Accelerator Keys with Glade Pin
Priyank Bolia11-Oct-05 21:12
MemberPriyank Bolia11-Oct-05 21:12 
GeneralCompile error Pin
Anonymous24-Aug-05 9:05
MemberAnonymous24-Aug-05 9:05 
AnswerRe: Compile error Pin
diego mesa tabares18-Aug-06 8:44
Memberdiego mesa tabares18-Aug-06 8:44 
Try to create a new enviroment variable called MONO_EXTERNAL_ENCODINGS and set it to default_locale

GeneralGtk# != Glade# Pin
Anonymous16-Jul-05 12:17
MemberAnonymous16-Jul-05 12:17 
GeneralRe: Gtk# != Glade# Pin
Priyank Bolia17-Jul-05 19:27
MemberPriyank Bolia17-Jul-05 19:27 
GeneralGTK# Start simple and build up. Pin
petethegeek12-Mar-05 1:32
Memberpetethegeek12-Mar-05 1:32 
GeneralRe: GTK# Start simple and build up. Pin
Priyank Bolia12-Mar-05 2:05
MemberPriyank Bolia12-Mar-05 2:05 
GeneralNice topic but... Pin
SHaroz6-Mar-05 16:39
MemberSHaroz6-Mar-05 16:39 
GeneralRe: Nice topic but... Pin
Priyank Bolia6-Mar-05 18:32
MemberPriyank Bolia6-Mar-05 18:32 
GeneralRe: Nice topic but... Pin
SHaroz6-Mar-05 19:08
MemberSHaroz6-Mar-05 19:08 
GeneralRe: Nice topic but... Pin
Priyank Bolia6-Mar-05 20:07
MemberPriyank Bolia6-Mar-05 20:07 

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.

Posted 6 Mar 2005


26 bookmarked