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 ();
}
}
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 ();
}
}
Compile it with command:
Mcs-pkg: glade-sharp-2.0-resource: gui. 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 ";
}
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)
{
}