Guide for Building C# Apps on Ubuntu: Graphical Applications using Windows Forms

Introduction and Background

I took a more than necessary break in posting the content and articles for Ubuntu programming in C#. Anyways, I am continuing from the same place where I left and as I already taught  we were at the final stages of sharing the guide for programming C# on Ubuntu; which can be thought of as a Linux environment. The previous posts can be found easily on my blog and on some of the networks that I support.

Previously, we had already talked about NuGet gallery packages, cryptographic helpers and much more. Whereas, in this post I am going to walk you people through using Windows Forms framework on Mono framework.

Windows Forms framework

Now, who doesn’t know Windows Forms framework in today’s world? If you have ever wanted to or have developed the C# programs or .NET applications then you are already familiar with Windows Forms application. Windows Forms is an old framework with graphical capabilities, and has been in the game for a more than a decade by now! It has started to get replaced by Windows Presentation Foundation, but nonetheless, it is still very popular and is being used widely by many software developers and many software development companies are still relying on it.

There are many benefits of using Windows Forms in your regular development teams,

  1. It supports graphics, not just the CPU and RAM processing.
  2. It can be used with resources for localization support is excellent.
  3. Provides a better way for drawing graphics on canvas.
  4. Event handling, delegate generation.
  5. Asynchronous programming patterns.
  6. Good data binding support provided.

I am going to provide you with some good references that you can use to learn more about Windows Forms, if sadly, you have no idea about the Windows Forms framework. However, one thing is for sure. Using Windows Forms is one of the easiest ways of building the graphical applications in ,NET environment.

References

Follow the following links and learn more about Windows Forms:

  1. Windows Forms
  2. Getting Started with Windows Forms
  3. Windows Forms Overview

Graphical applications in Mono Project

We have until now, discussed and learned how to build small programs and some very common applications that use special kind of algorithms, such as cryptographic helpers which can be used to encrypt and decrypt the data in Ubuntu using C#. Now, until this we haven’t covered graphical applications development in Mono, because that is where this framework lags behind. So, in this post we would like to learn how to build applications with graphical capabilities too. But, first, let’s talk about the frameworks provided in Mono Project itself.

  1. GTK#

    Which is a similar one to the GTK+ that C++ programmers have. This is a graphical toolkit that compiles down to the native code generation. It supports the native look and feel and can be used. But, it is somewhat difficult to learn!

    control application example
    Figure 1: GTK# control application example

  2. Windows Forms

    This is the topic that we are going to cover in this post.

  3. Other tools?

    Not yet, Mono Project is not ready to implement other frameworks yet.
    The thing is, Mono Project doesn’t have an excellent UI designer as we have in Visual Studio.

    Visual Studio UI Designer
    Figure 2:
    Visual Studio UI Designer

In Mono Project and Xamarin Studio, you are not provided with the similar tools until now. However, the designer is just the wrapper around the back-end code available. Xamarin Studio does have a good designer, but that works with GTK# tools and controls only. Not in the case of Windows Forms.

Applying a quick hack!

If you have ever programmed for Windows Forms, then you will know that Windows Forms is actually a bunch of objects with many fields and properties, which run and work to render the graphics on the screen. The same is applicable here. We would be using the codes to render the stuff on the screen. Basically, everything that you have on the screen is a field in the backend class, such as “Button”, “TextBox”, and “Label” etc. These are all going to be used, but, we are going to write the codes for this, instead of having a designer build them up for us.

Building the UI with Windows Forms

So, I hope using the hack is clear to you, after all, it is not a hack it is the actual way in which Windows Forms applications are constructed, built and executed. Only difference is that on Windows we also get to use the UI designer. In case of Mono Project, we would have to craft the UI with our own codes and then re-implement it if there is a changing to be performed on the screen later. Thus, putting it simply, we are going to implement it the hard way since there is no, “Drag-and-drop” method here.

To get started, you need to understand that a Windows Forms application is a C# application too. So, only thing to understand is C# language itself and the .NET framework. We are going to use the same concepts of .NET framework and then implement the same concepts to Mono to get our graphical applications ready.

Create project - any type

I have chosen to build this application on the top of Console application. You can select other types too. But, my recommendation is using the Console application.

Selecting the console project as the type
Figure 3: Selecting the console project as the type

Enter the names and details and create the project.

Entering the details for the project
Figure 4:
Entering the details for the project

At this moment, you will be at the same stage where you were in the beginning of this module, and the previous ones. You will have an empty project that only renders, “Hello world”. That is the start, from here we will continue to build the larger and complex stuff.

Hello world
Figure 5: Hello world!

This program is the default program that every time we were shown. Now, we need to add the graphics to the console. That’s simple. All we need to do is Read the next section.

Adding graphics to the console

Adding the graphics to the console is pretty much simple, you just need to do the trick as:

  1. Include the System.Windows.Forms namespace to the project.
  2. Include the System.Drawing namespace to the project.
  3. Import the packages to your source code.
  4. Write the code yourself; since we do not have the designer.
  5. Run the program, and you will see the program start up.

But, I am going to show you how to do that in a minute or two. But first, let us understand why Windows Forms can be build using the code itself. The thing is, the Windows Forms applications are started on a thread, the thread gets the graphics objects and bitmaps which are rendered on the screen later. The objects are simply instances of the classes, for instance of Button class. These are all passed to the Application object, and they are executed, upon execution they render the graphics on the screen.

Another thing to understand and note is that Windows Forms uses System.Drawing to draw the native code in Mono. Mono targets the objects using the assemblies provided in System.Drawing namespace, and then renders the objects on the screen. At the moment, Mono supports on Win32 theme for the objects. In the end of this post, you will see how your objects are going to look. Remember, the objects are instances.

Open the references and add the references to System.Windows.Forms and System.Drawing namespaces,

Initially references are like this
Figure 6: Initially references are like this

Adding the references to the namespaces
Figure 7: Adding the references to the namespaces

Now that we have everything set up, we can now continue to create a new Windows Form. This method is not similar to what we had in the previous IDEs, like Visual Studio. Instead, we are going to just mimic having a Windows Form.

Adding the Windows Form to your project

First thing to note is that there is no way of creating the form by itself in Mono Project. So, we are going to create a new object, and then we are going to turn it into the form and then render it using the main function. That’s just similar to what happens in Visual Studio too. You create the form, whose graphics are rendered separately in a designer, and the code file is generated separately. Visual Studio maintains everything for you, which is why you do not feel anything different.

Step 1: Create a new class, name it what you like, I chose MyForm and then write the following code to it.

  1. using System;  
  2. usingSystem.Windows.Forms;  
  3. namespaceGraphicalApp  
  4. {  
  5.     public class MyForm: Form  
  6.     {  
  7.         // No properties.  
  8.         publicMyForm()  
  9.         {  
  10.             // Default constructor  
  11.         }  
  12.         // No functions.  
  13.     }  
  14. }  
This is it. This is out, Windows Form now.

Step 2:
Before running the program, you also need to make sure that the main function now calls this form to be executed in the application too.
  1. // In the references  
  2. usingSystem.Windows.Forms;  
  3.   
  4. // In the main function  
  5. Application.Run(new MyForm());  
Now, run the project and you will (should!) see the following window with no controls, but with a graphical notation and rendering.

Sample empty Windows Form application
Figure 8: Sample empty Windows Form application

This shows that our project is ready to accept the objects for graphical components. We can now write the code for the objects that we need to render. For example and for the sake of simplicity I would like to create a sample form that renders the form, which would ask me about my name and then would greet me once I click on the button.

All of these are core C# programming concepts and this has nothing to do with the Windows Forms or any other fundamental concept. Instead, these are very simple and yet easy features of C# programming language itself.

Step 3: Anyways, to update, we can just update the MyForm class and write the code that we would need to have in order to render the form.
  1. using System;  
  2. usingSystem.Windows.Forms;  
  3. usingSystem.Drawing;  
  4. namespaceGraphicalApp  
  5. {  
  6.     public class MyForm: Form  
  7.     {  
  8.         // Properties.  
  9.         private Label label;  
  10.         privateTextBoxmyName;  
  11.         private Button btn;  
  12.         publicMyForm()  
  13.         {  
  14.             // Call the function to render the objects.  
  15.             Text = "Windows Forms app";  
  16.             this.Size = new Size(300, 350);  
  17.             render();  
  18.         }  
  19.         private void render()  
  20.         {  
  21.             label = new Label  
  22.             {  
  23.                 Text = "Your name: ", Location = new Point(10, 35)  
  24.             };  
  25.             myName = new TextBox  
  26.             {  
  27.                 Location = new Point(10, 60), Width = 150  
  28.             };  
  29.             btn = new Button  
  30.             {  
  31.                 Text = "Submit", Location = new Point(10, 100)  
  32.             };  
  33.             btn.Click += Btn_Click; // Handle the event.  
  34.             // Attach these objects to the graphics window.  
  35.             this.Controls.Add(label);  
  36.             this.Controls.Add(myName);  
  37.             this.Controls.Add(btn);  
  38.          }  
  39.         // Handler  
  40.         voidBtn_Click(object sender, EventArgs e)  
  41.         {  
  42.             MessageBox.Show("Hello, " + myName.Text + "!");  
  43.         }  
  44.     }  
  45. }  
Now again run the program, and you will see the following graphical application being rendered on the screen for you!

Windows Forms application on Ubuntu
Figure 9: This is the Windows Forms application on Ubuntu

Entering the data and seeing that it works
Figure 10: Entering the data and seeing that it works

Now, if you have been an old geek for Microsoft and Windows, then you will know that this style existed for decades in Windows environment. This style was typically adopted for Windows programming with C++, especially Win32 and then later C# was introduced and they had a different style; create a WPF app and look for yourself!

This is how Mono does the trick for Windows Forms. It does support Windows Forms and can use the same power of .NET framework to provide the similar ways to build the graphical applications for Linux environment. The benefit is that, you can now execute, build and run these graphical applications on Windows, Linux (every distro that supports Mono runtime) and Mac OS X. What more could you ask for?

Points of Interest

In this post, I have walked you through different stages of building the graphical applications on Ubuntu using C# in Mono Project. The benefit of using graphical applications is that you can do more in less area, because “A picture worth a thousand words!” Windows Forms is a framework that has been widely used by programmers of C# language and is still being used. If you can port your applications of Windows Forms to other platforms, it would be much better.

In this post, I have shown you how to do that. You can port your Windows Forms application, by porting the code for that application project, and then building it under Mono compilers.

The Mono runtime however, only supports Win32 theme for controls and however, provides a versatile amount of features, incredibly, they support all the new features of Windows Forms too. Which means, you can work around with:

 

  1. Graphics
  2. Multimedia
  3. Content management
  4. Data binding
  5. Event handling
  6. Asynchronous programming

And much more. There are more posts about Mono framework to come, but we are getting closer to having the guide completed and to be ready as a complete guide for “Programming C# on Linux”; or Ubuntu, what-ever you prefer.

Read more articles on C#:


Similar Articles