GTK#
HelloWorld, first try
HelloWorld in Gtk#:
using Gtk;
using GtkSharp;
using System;
class Hello {
static void Main()
{
Application.Init ();
Window window = new Window ("helloworld");
window.Show();
Application.Run ();
}
}
|
 |
compile:
mcs helloworld.cs -r gtk-sharp.dll -r glib-sharp.dll
It's a bit longer than console hello world and needs some
explanation.
Before we jump in the Main method, every GTK# Application will have
to import the namespaces:
using Gtk;
using GtkSharp;
If you don't do so, each Class from one of these namespaces needs
the namespace mentioned as prefix.
In Main() at first you see:
Application.Init()
This initializes GTK and is needed in every GTK# Application.
The next line create a new window. GTK# can set the title in the
constructor and saves one line compared to C.
Window window = new Window ("helloworld");
Application.Run()
shows the application and most importantly keeps it open, waiting
for events, until Quit() is called.
HelloWorld, second try
While the above program compiles and runs, it's doesn't quit,
properly. You've to exit with CRTL+C.
using Gtk;
using GtkSharp;
using System;
class Hello {
static void Main()
{
Application.Init ();
Button btn = new Button ("Hello World");
btn.Clicked += new EventHandler (hello);
Window window = new Window ("helloworld");
window.DeleteEvent += new DeleteEventHandler (delete_event);
window.Add (btn);
window.ShowAll ();
Application.Run ();
}
static void delete_event (object obj, DeleteEventArgs args)
{
Application.Quit ();
}
static void hello (object obj, EventArgs args)
{
Console.WriteLine("Hello World");
Application.Quit ();
}
}
This sample quits correctly.
GTK is an event driven toolkit, which means it will sleep in
Application.Run() until an event occurs and control is passed to
the appropriate function. GTK# can make use of the type "event".
When you close HelloWorld, the DeleteEvent is thrown, by the
window. To enable your application to react on the DeleteEvent, you
must connect it to an event handler.
window.DeleteEvent += new DeleteEventHandler (delete_event);
An event handler is passed an object, the object that fired the
event, here window, and EventArgs. The EventArgs here have the
special type DeleteEventArgs).
static void delete_event (object obj, DeleteEventArgs args)
{
Application.Quit ();
}
This sample also adds a button to the window and connects the
clicked event to "hello".
Last but not least the complete example in MonoBasic...
imports System
imports Gtk
imports GtkSharp
Public Module ToggleButtons
Sub OnDeleteEvent ( ByVal obj as object, ByVal args as DeleteEventArgs )
Application.Quit()
End Sub
Sub OnExitButtonEvent ( ByVal obj as object, ByVal args as EventArgs )
Application.Quit()
End Sub
Public Sub Main ()
Application.Init()
Dim window as Window = new Window("Toggle Buttons")
AddHandler window.DeleteEvent, AddressOf OnDeleteEvent
window.BorderWidth = 0
Dim box1 as VBox = new VBox (false, 10)
window.Add(box1)
box1.Show()
Dim box2 as VBox = new VBox (false, 10)
box2.BorderWidth = 10
box1.PackStart(box2, true, true, 0)
box2.Show()
Dim toggleButton as ToggleButton = new ToggleButton ("Button 1")
box2.PackStart(toggleButton, true, true, 0)
toggleButton.Show()
Dim toggleButton2 as ToggleButton = new ToggleButton ("Button 2")
toggleButton2.Active = true
box2.PackStart(toggleButton2, true, true, 0)
toggleButton2.Show()
Dim separator as HSeparator = new HSeparator ()
box1.PackStart(separator, false, true, 0)
separator.Show()
Dim box3 as VBox = new VBox (false, 10)
box3.BorderWidth = 10
box1.PackStart(box3, false, true, 0)
Dim button as Button = new Button("Close")
AddHandler button.Clicked, AddressOf OnExitButtonEvent
box3.PackStart(button, true, true, 0)
button.CanDefault = true
button.GrabDefault()
button.Show()
window.ShowAll()
Application.Run()
End Sub
End Module