My first Microsoft Form App in C#
24. 09. 2021
•
3 min
Within the .Net Framework, there is a library called Windows Forms. The Windows Forms library allows us to create graphical desktop apps using the C# programming language. Another name for this library is WinForms.
Applications created with the WinForms library are called Windows Forms Applications and only run on Windows.
Prerequisites:
- Installed Visual Studio 2019 with Desktop Developments Workload.
Creating a new project
When we start Visual Studio 2019, a window with four options appears. We select the option "Create a new project".
In the following, we will have to choose what kind of application we want to create. In our case, when we want to create a Windows Forms App, we look for the Windows Forms App (.NET Framework). Continue by pressing the "Next" button.
In the Configure your new project window, fill in the required information, name our project, select where the project will be saved ... Continue by pressing the "Create" button.
After creating a new project, you should see a window like the one below.
Creating an application
We will create a simple application that will allow us to convert a number to Roman.
We start creating our application by customizing the user interface. We do this by clicking on the window of our application.
In the Properties part of the window, we can change the look of our application. We can change the app name and icon in the corner, the color scheme of the application ...
The following is the addition of the widgets of our application.
Widgets used:
- Number entry field
- Label for display of a Roman numeral
- Convert button for start converting process
The widgets can be found in the upper left corner. Add widgets to our window by dragging it from the Toolbox to the Main window.
After inserting the widgets, our program looks something like this in the end. I recommend that we change the name of all the elements so that we know what each element represents.
Now, we can add some functions to our app. This is done by double-clicking the button. A new window will open in which you can enter the code.
The final code is like the following.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RomainConverter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string ToRoman(int number)
{
if ((number < 0) || (number > 3999)) return "Value must be between 1 and 3999";
if (number < 1) return string.Empty;
if (number >= 1000) return "M" + ToRoman(number - 1000);
if (number >= 900) return "CM" + ToRoman(number - 900);
if (number >= 500) return "D" + ToRoman(number - 500);
if (number >= 400) return "CD" + ToRoman(number - 400);
if (number >= 100) return "C" + ToRoman(number - 100);
if (number >= 90) return "XC" + ToRoman(number - 90);
if (number >= 50) return "L" + ToRoman(number - 50);
if (number >= 40) return "XL" + ToRoman(number - 40);
if (number >= 10) return "X" + ToRoman(number - 10);
if (number >= 9) return "IX" + ToRoman(number - 9);
if (number >= 5) return "V" + ToRoman(number - 5);
if (number >= 4) return "IV" + ToRoman(number - 4);
if (number >= 1) return "I" + ToRoman(number - 1);
throw new ArgumentOutOfRangeException("Value must be between 1 and 3999");
}
private void ConvertButton_Click(object sender, EventArgs e)
{
var isNumeric = int.TryParse(InputNumber.Text, out int n);
Result.Text = ToRoman(n);
}
}
}
Now we can test our app how it works. This is done by pressing the "Start" button in the top taskbar.
After clicking, a new window will open in which we can test our application.
I hope the post helped you. For even more content, you can follow me on my Twitter.