Home  Contents

First steps in Mono Winforms

In this part of the Mono Winforms tutorial, we introduce some basic programs in Winforms programing library.

Simple

This is a simple Winforms application.

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());
    }
}

This code example shows a small window on the screen.

 using System.Windows.Forms;
 using System.Drawing;

Here we use the using directive, which allows us to use a type from an appropriate namespace without a fully qualified name. For example, we can now write Form instead of System.Windows.Forms.Form.

 public class Simple : Form
 {
 ...
 }

In Winforms, any window or a dialog is a Form. This control is a basic container, whose purpose is to display other child controls. Our class, Simple, inherits from a form. This way it becomes a form itself.

 Text = "Simple";
 Size = new Size(250, 200);

Text and Size are properties of a form. Changing these properties, we modify our form control. The first line displays text "Simple" in the titlebar of the form control. The second line sets the size of the form to 250x200 px.

 CenterToScreen();

This method centers our application on the screen.

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

When compiled and run, the Main method is executed first. The code instantiates the Simple class and runs it.

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

Here is how we compile the source code. If we didn't make any mistakes, we should have simple.exe file in our current working directory.


Simple
Figure: Simple

Icon

Mono means monkey in Spanish. If we do not provide an icon for our application, we have a head of a monkey by default. The next example shows, how to change this.

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

public class MForm : Form
{
    public MForm()
    {
       Text = "Icon";
       Size = new Size(250, 200);

       try {
           Icon = new Icon("web.ico");
       } catch (Exception e) {
           Console.WriteLine(e.Message);
           Environment.Exit(1);
       }

       CenterToScreen();
    }

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

The code example shows an icon in the upper left corner of the form. A form's icon is the picture that represents the form in the taskbar as well as the icon that is displayed for the control box of the form.

 try {
     Icon = new Icon("web.ico");
 } catch (Exception e) {
     Console.WriteLine(e.Message);
     Environment.Exit(1);
 }

It is a good practice to put all input output work between the try/catch keywords. The web.ico file must be available in the current working directory. This is the directory from where we execute (./icon.exe) our application.


Icon
Figure: Icon

Tooltips

A tooltip is a small rectangular pop-up window that displays a brief description of a control's purpose when the user rests the pointer on the control.

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

class MForm : Form {

    public MForm() {
        Text = "Tooltips";
        Size = new Size(250, 200);

        ToolTip btnTlp = new ToolTip();

        btnTlp.SetToolTip(this, "This is a Form");

        Button button = new Button();
        btnTlp.SetToolTip(button, "This is a Button Control");
        button.Text = "Button";
        button.Location = new Point(30, 70);
        button.Parent = this;

        CenterToScreen();
    }
}

class MApplication {
    static void Main() {
        Application.Run(new MForm());
    }
}

Our code example creates a tooltip for two controls. The Button control and the Form control.

 ToolTip btnTlp = new ToolTip();

Here we create the ToolTip control. This instance is used to provide tooltips for both controls.

 btnTlp.SetToolTip(this, "This is a Form");

Here we set a tooltip for a form.

 btnTlp.SetToolTip(button, "This is a Button Control");

And here for our button.

 Button button = new Button();
 btnTlp.SetToolTip(button, "This is a Button Control");
 button.Text = "Button";
 button.Location = new Point(30, 70);
 button.Parent = this;

Notice the creation of the Button control. The Text property is a label for the button. The Location property places the button on the form at x=30, y = 70 px coordinates. Finally, the Parent property determines the container, where the button will reside.


Tooltips
Figure: Tooltips

Button

Our last code example shows a button control in action.

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

class MForm : Form {
    public MForm() {
        Text = "Button";
        Size = new Size(250, 200);

        Button button = new Button();

        button.Location = new Point(30, 20);
        button.Text = "Quit";
        button.Click += new EventHandler(OnClick);
        button.MouseEnter += new EventHandler(OnEnter);

        Controls.Add(button);
        CenterToScreen();
    }

    void OnClick(object sender, EventArgs e) {
       Close();
    }

    void OnEnter(object sender, EventArgs e) {
       Console.WriteLine("Button Entered");
    }

}

class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

All GUI programming is event driven programming. In our example, we show a button control on a form container. The button will listen to two events. The Click and the MouseEnter events.

 button.Click += new EventHandler(OnClick);   

This code line plugs an event handler to the Click event. When we click on the button, the OnClick() method is called.

 button.MouseEnter += new EventHandler(OnEnter);

When we enter the button area with the mouse pointer, the MouseEnter event is triggerd. In this case, our code calls the OnEnter() method.

 void OnClick(object sender, EventArgs e) {
     Close();
 }

The method closes the application.

 void OnEnter(object sender, EventArgs e) {
     Console.WriteLine("Button Entered");
 }

When we enter the button control area with the mouse pointer, "Button Entered" text is displayed in the terminal.


This part of the Mono Winforms tutorial showed some introductory code examples to get you started with the Winforms programming library.