C# Windows Form

C# Windows Form Application Examples For Beginners

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.

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.Ā 

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.

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.

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.

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:

Output:

Example 4:

2 Comments

    • 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!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.