Table of contents
1.
Introduction
2.
Windows Forms
2.1.
Using Forms as Dialog Boxes
2.2.
Using Forms as Views
3.
The Basics
4.
Hello World in C#
5.
Frequently Asked Questions
5.1.
What are .NET and .NET Framework?
5.2.
What are the two main files found in the Solutions Explorer of a WinForms application?
5.3.
What are the two methods used to access the properties of Windows Forms?
6.
Conclusion
Last Updated: Mar 27, 2024

Windows Forms

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

.NET Framework is a popular technology that supports the building and running of Windows apps and web services. It provides a code-execution environment that minimizes software deployment and versioning conflicts. It eliminates the performance problems of scripted environments and allows users to develop different types of Windows-based and Web-based apps comfortably. The recent versions of .NET have introduced many changes to the standard application templates, including the Windows Forms templates for C#. Let's discuss Windows Forms in detail.

Recommended topics, Palindrome in C# and  Ienumerable vs Iqueryable

Windows Forms

Windows Forms was the original GUI framework for .NET. It is an easy UI framework for building rich client applications for Windows. This development platform supports various app development features such as controls, graphics, user input and many more. It creates only desktop-based apps that can access the local hardware and file system of the computer where it is running. Visual Studio enables users to create Windows Forms apps using a drag-and-drop graphic designer.

Using Forms as Dialog Boxes

Windows Forms are commonly used to create dialog boxes that interact with the user. Dialog boxes range from simple text boxes to elaborate forms with many controls. Dialog boxes display notification messages to the user. They have one or more buttons to dismiss the dialog box. The Windows operating system provides a message box control, a specialised dialog box that simplifies the task of displaying a simple text message to the user. The .NET Framework wraps the Windows message box in the MessageBox class.

Using Forms as Views

Windows Forms applications also include child windows and views that allow interaction between the user and the application. A significant difference between views and dialog boxes is that views usually don’t have button controls to dismiss the form. The Show() and Close() methods display and close a view.

The Basics

Windows Forms applications run only on the Windows desktop. The classes used to create such rich-client applications are found in the System.Windows.Forms namespace. This namespace includes the Form class, which constitutes the base class for all dialog boxes and top-level windows in a .NET WinForms application. It also contains classes that manage the controls and their interactions with the clipboard, menus, etc. 

A form is a visual screen that displays information to the user. A Windows Forms app is built by adding controls to forms and creating appropriate responses to user actions. A control is a unique UI element that displays data or accepts data input.

An action performed on a control triggers an event. There are controls to display text boxes, drop-down boxes, radio buttons, and even webpages. There is an option to create custom controls too.

Below is a diagram showing the label, TextBox and Button controls. Labels display the content to the user; textboxes allow the user to enter some data, and buttons, when clicked, are set to do some particular action. 

Hello World in C#

Let's build our first Hello World application on Windows Forms using Visual Studio.

Step 1: Download Visual Studio if you do not have it installed already. Be sure to select the .NET desktop development tools during installation. 

Step 2: Once installed, open Visual Studio and create a new Project. 

Step 3: Choose C# as the language and search for Windows Forms Apps. Click on Next.
 

Step 4: Open the Solutions explorer to look at the files involved in this WinForms application. The Toolbar can be displayed by clicking it on the left side of the window.

Step 5: Search for a Label component in the Toolbar. Drag and drop it in the form designer window.

Step 6: Righ-click on the label to open the properties pane. Here, you can change the label's text size, colour and style.

Step 7: Click the Start button to build and run the application. 

Frequently Asked Questions

What are .NET and .NET Framework?

.NET is a developer platform used to build various mobile, desktop, and Web applications. It supports multiple languages like C#, F#, Visual Basic, J#, C++, etc., for building such applications .NET framework is a software development platform that supports object-oriented programming and offers services like memory management, networking and security to its applications.

What are the two main files found in the Solutions Explorer of a WinForms application?

A form application called the Forms1.cs contains the code for the Windows Form application. The Main program file called the Program.cs is a default code file created when a new application is created in Visual Studio. This contains the startup code for the entire application.

What are the two methods used to access the properties of Windows Forms?

GET property accessor is a read-only property that returns the property value based on the different access levels. The SET property accessor can write or assign a value to a property based on the defined access level.

Conclusion

This article has extensively discussed Windows Forms. It gives a brief overview of what Windows Forms are and why are they used. We also learned to build a Hello World application using Windows Forms on Visual Studio.

Check out our blogs on C# Button ControlC# Label Control and C# TextBox control. Follow our guided path to C# Windows Forms here.

Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and Algorithms, Machine Learning, Deep Learning, and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio! 

Looking for questions from tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations.

 

Upvote our blogs if you find them insightful and engaging! Happy Coding!

Live masterclass