This is a quick tutorial for getting a first gtk-sharp app up and going.
Install MonoDevelop
$ sudo apt-get install monodevelop libgtk2.0-cil-dev
Start MonoDevelop
It’ll be in your GNOME application menu under Applications->Programming->MonoDevelop
Create a new Gtk# Project
Run project
You can run this boilerplate by pressing Ctrl-F5
Using the Designer
Double-click on the file named MainWindow.cs in the Solution section. When the file opens, click the Designer button below the code view.
The Toolbox
To add the Toolbox, press Alt-Shift-B or click View->Toolbox.
Drag a VBox widget from the Toolbox to the main window.
Drop a Menu Bar widget into the top cell of the VBox, and a Statusbar into the bottom.
Properties grid
Drop a Button widget into the middle cell of the VBox.
Add the Properties grid from the View menu (Alt+Shift+P or View->Properties).
Select the new button by clicking on it.
In the Properties grid, expand the Button Properties section and click the value of the Label field. Delete the default content and replace it with a different string.
Adding a Click handler
Select the button in the Designer view
In the Property grid, select the Signals tab
Click twice on the text in the column to the right of the Clicked and replace Click here to add a new handler with MyButtonClickHandler
Click on the Source Code button below the viewer window to switch to the C♯ code. You can use the find tool to search for the new handler you created, called MyButtonClickHandler. Ctrl-F will bring up the find dialogue.
Add some code to update the status bar.
protected virtual void MyButtonClickHandler (object sender, System.EventArgs e) { var contextId = this.statusbar1.GetContextId("clicked"); this.statusbar1.Push(contextId, "Clicky-Clicky" ); }
Modify button layout
The size of the running window is a little weird. I fixed mine by un-setting the Auto Size boolean and setting the Expand and Fill in the Box Child Layout section of the Properties grid.
Exercise button click handler
Run your application with Ctrl-F5 and click on the button. You should see the new string show up in the status bar at the bottom left of the window.
6 responses to “Getting started with gtk♯”
Thanks for the tutorial. Do you plan to release more tutorials on gtk#, preferably getting more in-depth? The documentation available (last time I looked) seemed a little hit-and-miss to me.
Yeah, I’d be happy to write more as time permits. One of my friends is looking to build an IM client and I wrote this up real quick to point him in the right direction. I should add a click handler to the button and display something in the status bar or something next… but work calls ;)
Very nice, thanks for you good work.
You lost your Gtk+ theme in the middle. What happened?
dist-upgrading lucid. gnome-settings-daemon crashed and I couldn’t be arsed to start it up again ;)
This tutorial is great. Would be nice a clear tutorial to make a Windows-ready tarball for a GTK# software.