Introduction to Mono Winforms

The first part of the Mono Winforms tutorial introduces the Mono platform and the Winforms library.

About this tutorial

This is Mono C# Winforms tutorial. Mono Winforms tutorial is for beginner programmers. The goal of this tutorial is to teach readers basics of GUI programming in Mono Winforms. The tutorial is created and tested on Linux. Nevertheless, it can be used on other operating systems as well. Most examples should run without modification. Images used in this tutorial can be downloaded here.

Mono

The Mono Project is an open development initiative sponsored by Xamarin to develop an open source, UNIX version of the Microsoft .NET development platform. It is a .NET compatible set of tools, which include C# compiler, Common Language Runtime, ADO.NET, ASP.NET and Winforms libraries.

Mono can be divided into three groups:

The core components are the C# language and the Common language runtime. The Gnome development stack includes the GTK# library and various database connectivity libraries. Finally the Microsoft compatibility stack includes the ADO.NET, ASP.NET and the Winforms libraries.

Mono is multiplatform programming platform. It can be run on Linux, BSD, Mac OS X, Solaris and Windows operating systems. It is a multilanguage effort. For now, only C# language is fully supported. Languages like Visual Basic or IronPython are not yet finished.

Winforms

Windows Forms is a graphical user interface application programming interface (API) included as a part of Microsoft's .NET Framework. As of 13 May 2008, Mono's System.Windows.Forms 2.0 is API complete. Simply put, Winforms is a library for creating GUI applications.

Compiling examples

Our tutorial uses the C# language. To compile all the examples in this tutorial, we use the gmcs compiler. The gmcs compiler implements the complete C# 2.0 specification including generics. C# source files must end with a .cs extension.

$ gmcs --about
The Mono C# compiler is (C) 2001-2008, Novell, Inc.
The compiler source code is released under the terms of the GNU GPL
For more information on Mono, visit the project Web site
   http://www.mono-project.com
The compiler was written by Miguel de Icaza, Ravi Pratap, Martin Baulig, 
Marek Safar, Raja R Harinath, Atushi Enomoto

We have a simple C# source file. We will discuss the source later. For now, we concentrate on the compilation process.

simple.cs
using System.Windows.Forms;
using System.Drawing;

public class Simple : Form
{
    public Simple()
    {
       Text = "Simple";
       Size = new Size(250, 200);
       CenterToScreen();
    }

    static public void Main()
    {
       Application.Run(new Simple());
    }
}

C# applications use modules called assemblies. The Mono C# compiler by default only references three assemblies: mscorlib.dll, System.dll and System.Xml.dll. Any additional assembly must be specified with a command line option of the compiler. In our simple example, we need System.Windows.Forms.dll and System.Drawing.dll assemblies.

 gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll simple.cs

Here we compile the simple.cs source file. If the output file name is not specified, we get the exe file with name equal to the source file name. In our example, simple.exe file.

 gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll simple.cs -out:simpleexample.exe

Here we specify the output file name. We compile source file and get simpleexample.exe file.

Reference

This was an introduction to the Mono Winforms library.