RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Mono Development 101: Build Linux Desktop Apps with .NET : Page 2

Mono and the MonoDevelop IDE let you build Linux desktop applications using the .NET Framework. Find out how an open source guy with minimal .NET experience did it.


Creating Your First Mono Project

Upon firing up MonoDevelop for the first time, I was surprised to see it was possible to create a GTK-based project (see Figure 1). The Mono-specific version is called Gtk# (Gtk Sharp), giving developers access to a rich suite of cross-platform widgets.

Mono Linux
Figure 1. Creating a Gtk# 2.0-based Project

After creating the new project, MonoDevelop will automatically provide the C#/Gtk# equivalent of "Hello, world" as a starting point. However, attempting to compile and run the skeleton application produced a number of errors regarding missing assemblies, all of which were pertinent to Gtk#. Additional research indicated that Gtk# is not installed by default alongside Mono. Another trip to the Synaptic Package Manager fixed the problem, and I subsequently successfully compiled and ran the application (see Figure 2).

Mono Linux
Figure 2. Running "Hello, World"

The next step involved adding a few interface elements to serve as starting points for subsequently adding features. Fortunately, MonoDevelop is equipped with a great interface designer, which you can access by clicking the Designer button located at the bottom of the editor (see Figure 3).

Mono Linux
Figure 3. Click the Designer Button to Open the Interface Designer

When clicked, a window containing almost fifty interface elements will appear. To design the interface, all you need to do is drag and drop the desired elements atop the window. If you're not familiar with the behavior of layout managers, take the time to read this tutorial explaining Gtk#'s layout approach. After adding a menu bar and status bar, I was starting to see glimpses of my application prototype (Figure 4).

Mono Linux
Figure 4. Adding a Menu and Status Bar

Wiring Events to the Controls

Continuing with the approach of continuous small victories, I next decided to implement the Quit command found in the File menu, and create a project description that appears when the user clicks the About RadioRadio command found in the About menu. Let's start with Quit.

Open the main window's interface designer and click the Quit menu item. When you do, the Properties window will appear (Figure 5).

Mono Linux
Figure 5. Setting the Quit Menu Item's Activated Signal

We want to tie a signal to the Activated event. When clicked, the method associated with the event will execute. Associate the name OnQuit with the Activated event. When you do, a method named OnQuit will automatically be added to the MainWindow.cs source. All you need to do is add the Application.Quit() call, as depicted here:

protected void OnQuit (object sender, System.EventArgs e)

Next, let's create the about dialog. A special type of window called an AboutDialog is native to Gtk#. It allows you to easily package all of the usual information about your application into a simple popup window, including the title, author and copyright data. Attach a method called OnShowAbout to the About menu item, and modify the OnShowAbout method so it looks like this:

protected virtual void OnShowAbout (object sender, System.EventArgs e)
  AboutDialog about = new AboutDialog();
  about.Title = "About RadioRadio";
  about.Copyright = "Copyright 2011 W. Jason Gilmore";

Rebuild and run the application. When you click on the About menu item, you'll be greeted with the window found in Figure 6.

Mono Linux
Figure 6. Creating an About Dialog

Where to From Here?

Mono and Gtk# are both actively developed projects boasting an enormous amount of documentation, making your investigatory work all the easier. Hopefully this short tutorial gave you some insight into how to get started!

If you want to keep tabs on my progress with RadioRadio, follow me on GitHub, as I'll be posting the files soon.

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date