Intro to GTK with C#

Written By: James Williams

- 10 May 2006 -

Description: Why would anyone want to use GTK# when they can use Windows.Forms? It's cross-platform and more complete than the Mono implementation of Windows.Forms. GTK is also ingrained in the Linux community as the basis of one of the popular user interface systems, GNOME.

  1. Hello World
  2. Event Handlers
  3. On to the Code
  4. Delegates

Event Handlers

Any component can have any number of events associated with it such as activate, recieveFocus, lostFocus, disposed, and so on. Event handlers allow specific methods subscribe to an event and get called whenever that event is raised. In Glade, the event handler stubs take the form of on_componentName_event. For example, the quit menu item will probably have a event handler stub named on_quit1_activate. These are the main hooks from the GUI to your application code. We can leave the names as it or change them to match the particular nomenclature of the language that will consume the XML file.

Click the obnoxiously big A from the palette and click the second vertical box. Click the Widgets tab and change the label to "Hello World!"[minus the quotes]. Leave the name the same. We want the label to take up as much space as possible so set the Expand and Fill buttons to Y in the Packing tab of the Properties window.

Click Here To View A Screenshot

Next, we raised OK button from the palette and drop it into the third box. Change the label to "Click Me!" and the name to btnClickMe. Under the Signals tab of the Properties window, click the ellipsis (...) next to Signal and select "clicked" from the list. Leave the default handler name.

Click Here To View A Screenshot

Click Here To View A Screenshot

Click Here To View A Screenshot

Lastly, we need to handle users clicking the "X" to close the window. With no event handler tied to it, it will close the window but not exit the application. Select the window and click the ellipsis on the Signals tab, select delete_event from the GtkWidget area. Save the file and we're done.

Click Here To View A Screenshot

Click Here To View A Screenshot

<< Previous

Next >>