Choosing Gtk# or Glade#
Gtk# looks like Windows Forms and Controls, and Glade# is more like WPF, it can define the widgets in a xml file, and load it into the application.
The Gtk# "Hello world" application.
using System;
using Gtk;
public class GtkHelloWorld {
public static void Main() {
Application.Init();
//Create the Window
Window myWin = new Window("My first GTK# Application! ");
myWin.Resize(200,200);
//Create a label and put some text in it.
Label myLabel = new Label();
myLabel.Text = "Hello World!!!!";
//Add the label to the form
myWin.Add(myLabel);
//Show Everything
myWin.ShowAll();
Application.Run();
}
}
using Gtk;
public class GtkHelloWorld {
public static void Main() {
Application.Init();
//Create the Window
Window myWin = new Window("My first GTK# Application! ");
myWin.Resize(200,200);
//Create a label and put some text in it.
Label myLabel = new Label();
myLabel.Text = "Hello World!!!!";
//Add the label to the form
myWin.Add(myLabel);
//Show Everything
myWin.ShowAll();
Application.Run();
}
}
compile it with command:
mcs -pkg:gtk-sharp-2.0 helloword.cs
To use a glade file:
// file: glade.cs
using System;
using Gtk;
using Glade;
public class GladeApp
{
public static void Main (string[] args)
{
new GladeApp (args);
}
public GladeApp (string[] args)
{
Application.Init();
Glade.XML gxml = new Glade.XML (null, "gui.glade", "window1", null);
gxml.Autoconnect (this);
Application.Run();
}
}
using System;
using Gtk;
using Glade;
public class GladeApp
{
public static void Main (string[] args)
{
new GladeApp (args);
}
public GladeApp (string[] args)
{
Application.Init();
Glade.XML gxml = new Glade.XML (null, "gui.glade", "window1", null);
gxml.Autoconnect (this);
Application.Run();
}
}
compile it with command:
mcs -pkg:glade-sharp-2.0 -resource:gui.glade glade.cs
use -resource:gui.glade option to make it as embedded resource. Or just leave it in the file system,
and use the following code to load it.
Glade.XML gxml = new Glade.XML ("./gui.glade", "window1", null);
add private member, then they can be used in the program, and can add event handler to catch widget events.
[Widget]
Button button1;
[Widget]
Label label1;
button1.Clicked += OnPressButtonEvent;
public void OnPressButtonEvent( object o, EventArgs e)
{
Console.WriteLine("Button press");
label1.Text ="Mono";
}
Button button1;
[Widget]
Label label1;
button1.Clicked += OnPressButtonEvent;
public void OnPressButtonEvent( object o, EventArgs e)
{
Console.WriteLine("Button press");
label1.Text ="Mono";
}
or add the widget events in the glade designer, and add the handler code with the same method name:
void on_button1_clicked(object o, EventArgs e)
{
}
{
}