In this article, we’ll learn How to how to create a Windows Forms Application in Visual Studio with C#.
Create a New Project
First, you’ll create a C# application project. The project type comes with all the template files you’ll need, before you’ve even added anything.

On the Create a new project window, choose the Windows Forms App (.NET Framework) template for C#.
(If you prefer, you can refine your search to quickly get to the template you want.)

In the Configure your new project window, type or enter HelloWorld in the Project name box. Then, choose Create.

After you select your C# project template and name your file, Visual Studio opens a form for you. A form is a Windows user interface.

On the left-hand side of Visual Studio, you will also see a ToolBox. The toolbox contains all the controls which can be added to a Windows Forms. Controls like a text box or a label are just some of the controls which can be added to a Windows Forms.

C# Windows Form Examples
Example 1 : Add Two Numbers in C# Windows Form Application.
The following example creates a C# windows Form application with two text boxes, one button,four label controls. When user enters two numbers in to first two text boxes and clicks on button1 then sum has to be calculated for those two numbers and display the result in label4.
Step 1:
Create a windows application and design the form as follows.
Open Visual Studio ->File -> New Project ->Visual C#-> select Windows Forms Application
Give the name of the application and click on OK.

Step 2:
Set the following properties for the controls on the form.
label1 >> Text: First Number
label2 >> Text: Second Number
label3 >> Text: Result
label4 >> Text: Name: lblResult
button1 >> Text: Sum Name: btnSum
textBox1>> Name: txtNumber1
textBox2>> Name: txtNumber2
Step 3:
Double click on the sum button and write the following code in the click event of that button to calculate sum and display the result in result textbox.
1 2 3 4 5 6 7 8 9 10 | private void btnSum_Click(object sender, EventArgs e) { int num1, num2, sum; num1=Convert.ToInt32(txtNumber1.Text); num2 = Convert.ToInt32(txtNumber2.Text); sum = num1 + num2; lblResult.Text = sum.ToString(); } |
Output:

Example 2: MessageBox.Show Method in C#
MessageBox is a class in C# and Show is a method that displays a message in a small window in the center of the Form.Ā MessageBox is used to provide confirmations of a task being done or to provide warnings before a task is done.Ā Create a Windows Forms app in Visual Studio and add a button on it. Something like this below.Ā Ā

Let’s say, you want to show a message on a button click event handler. Here is the code for that.Ā

Note:Ā By default the OK Button will be shown.
MessageBox with Title
The following code snippet creates a simple MessageBox with a title.Ā
1 2 3 4 5 | string message = "Simple MessageBox"; string title = "Title"; MessageBox.Show(message, title); |

MessageBox with Buttons
A MessageBox can have different button combinations such as YesNo and OKCancel. The MessageBoxButtons enumeration represents the buttons to be displayed on a MessageBox and has following values.
- OK
- OKCancel
- AbortRetryIgnore
- YesNoCancel
- YesNo
- RetryCancel
The following code snippet creates a MessageBox with a title and Yes and No buttons. This is a typical MessageBox you may call when you want to close an application. If the Yes button is clicked, the application will be closed. The Show method returns a DialogResult enumeration.
1 2 3 4 5 6 7 8 9 10 11 | string message = "Do you want to close this window?"; string title = "Close Window"; MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result = MessageBox.Show(message, title, buttons); if (result == DialogResult.Yes) { this.Close(); } else { // Do something } |

MessageBox with Icon
A MessageBox can display an icon on the dialog. A MessageBoxIcons enumeration represents an icon to be displayed on a MessageBox and has the following values.
- None
- Hand
- Question
- Exclamation
- Asterisk
- Stop
- Error
- Warning
- Information
The following code snippet creates a MessageBox with a title, buttons, and an icon.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | string message = "Do you want to abort this operation?"; string title = "Close Window"; MessageBoxButtons buttons = MessageBoxButtons.AbortRetryIgnore; DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning); if (result == DialogResult.Abort) { this.Close(); } elseif(result == DialogResult.Retry) { // Do nothing } else { // Do something } |

MessageBox with Default Button
We can also set the default button on a MessageBox. By default, the first button is the default button. The MessageBoxDefaultButton enumeration is used for this purpose and it has the following three values.
- Button1
- Button2
- Button3
The following code snippet creates a MessageBox with a title, buttons, and an icon and sets the second button as a default button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | string message = "Do you want to abort this operation?"; string title = "Close Window"; MessageBoxButtons buttons = MessageBoxButtons.AbortRetryIgnore; DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2); if (result == DialogResult.Abort) { this.Close(); } elseif(result == DialogResult.Retry) { // Do nothing } else { // Do something } |

Example 3: Calculate Area and Perimeter of a Rectangle Using C# Windows Form Application
In this example, weāll learn How to calculate area and perimeter of a rectangle in C# WinForm.Ā To find theĀ areaĀ of aĀ rectangleĀ or a square you need to multiply the length and the width of aĀ rectangle.
Form Design:

Source Code:
1 2 3 4 5 6 7 8 9 10 11 | private void button1_Click(object sender, EventArgs e) { int width = Convert.ToInt32(txtWidth.Text); int length = Convert.ToInt32(txtLength.Text); int area = width * length; int perimeter = (width + length) * 2; lblPerimeter.Text = "Perimeter : " + perimeter; lblArea.Text = "Area : " + area; } |
Output:

Concise, understandably instructive and very useful. Commendable job!!! THANKS.
I’m thrilled to hear that you found the information concise and helpful! If you have any more questions or if there’s anything else I can assist you with, feel free to let me know. Thanks for your kind words!