2. Introduction
● Windows Forms is a Graphical User Interface(GUI) class library which is
bundled in .Net Framework.
● Its main purpose is to provide an easier interface to develop the applications
for desktop, tablet, PCs. It is also termed as the WinForms.
● The applications which are developed by using Windows Forms or WinForms
are known as the Windows Forms Applications that runs on the desktop
computer.
● WinForms can be used only to develop the Windows Forms Applications not
web applications. WinForms applications can contain the different type of
controls like labels, list boxes, tooltip etc.
3. 3
Contrasting Windows and Console Applications
● Windows applications function differently from console applications.
● Windows applications look differently from console applications.
4. 4
Contrasting Windows and Console Applications by
Functionality
● Console applications
○ Each line in Main( ) executed sequentially – then the program halts
● Windows applications
○ Once launched, sits and waits for an event
○ Sits in a process loop
● Event: notification from operating system that an action, such as the user
clicking the mouse or pressing a key, has occurred
○ Write event-handler methods for Windows apps
5. 5
Graphical User Interfaces
● Windows applications also look different from console applications
● Interface: front end of a program
○ Visual image you see when you run a program
● Graphical user interface (GUI) includes:
○ Menus
○ Text in many different colors and sizes
○ Other controls (pictures, buttons, etc.)
6. 6
Windows Applications
● Reference and import System.Windows.Forms
namespace
● Class heading definition
○ Includes not only the class name, but a
colon followed by another class name
■ Derived class (first class), Base class
(second class)
■ public class Form1 : Form
● Derived classes inherit from base class
● No multiple inheritance within .NET languages
7. 7
Windows Applications (continued)
● Text - property of the Form class
○ A property for setting/getting title bar caption
○ Can be used in constructor
● Windows forms/controls offer many properties
including Text, Color, Font, and Location
● Execution begins in Main( ) method
○ Main( ) is located in Program.cs file for the
application
○ Call to Run( ) method places application in
process loop
8. Creating a Windows Forms Application Using Visual Studio
2022
● Open the Visual Studio then Go to File -> New -> Project to create a new
project and then select the language as Visual C# from the left menu.
● Click on Windows Forms App(.NET Framework) in the middle of current
window. After that give the project name and Click OK.
12. using System.Windows.Forms; // Line 1
namespace Windows0
{
public class Form1 : Form // Line 2
{
public Form1( ) // Line 3
{
Text = "Simple Windows Application"; // Line 4
}
static void Main( )
{
Form1 winForm = new Form1( ); // Line 5
Application.Run(winForm); // Line 6
}
}
}
New
namespace
referenced
Constructor
Base class
Sets
title bar
caption
Starts
process
loop
13. Project Windows
After that following window will display which will be divided into three parts as follows:
● Editor Window or Main Window: Here, you will work with forms and code
editing. You can notice the layout of form which is now blank. You will double click
the form then it will open the code for that.
● Solution Explorer Window: It is used to navigate between all items in solution.
For example, if you will select a file from this window then particular information
will be displayed in the property window.
● Properties Window: This window is used to change the different properties of the
selected item in the Solution Explorer. Also, you can change the properties of
components or controls that you will add to the forms.
14. 14
Windows Forms
● Extensive collection of Control classes
● Top-level window for an application is called a Form
● Each control has collection of properties and methods
○ Select property from an alphabetized list (Properties window)
○ Change property by clicking in the box and selecting or typing the new
entry at design time.
● Each control has collection of events.
15. Adding Controls to a Form
● To add controls to your WinForms application go to Toolbox tab present in the
extreme left side of Visual Studio. Here, you can see a list of controls. To
access the most commonly used controls go to Common Controls present in
Toolbox tab.
● Drag and drop the controls that you needed on created Form. For example, if
you can add TextBox, ListBox, Button etc. as shown below. By clicking on
the particular dropped control you can see and change its properties present
in the right most corner of Visual Studio.
16. Form Controls
● In the above image, you can see the TextBox is selected and its properties
like TextAlign, Font etc. are opened in right most corner. You can change its
properties’ values as per the application need.
● The code of controls will be automatically added in the background. You can
check the Form1.Designer.cs file present in the Solution Explorer Window.
20. Inspecting the Code Generated by Visual Studio
● Three source code files
ending with a .cs
extension are part of the
application
20
Expand Form1.cs
node to reveal the
Form1.Designer.c
s file
21. 21
Simple Windows Application
● IDE separates the source code into three separate files
○ Form1.cs: normally this is the only one you edit
○ Form1.Designer.cs: holds the auto generated code
○ Program.cs: contains the Main( ) method, where execution always
begins
● Form1.cs and Form1.Designer.cs both include partial class definitions for the
Form1 class
22. Inspecting the Code - Form1.cs
● Number of namespaces automatically added, including
System.Windows.Forms
● Constructor calls InitializeComponent( ) method
public Form1( )
{
// Required for Windows Form Designer support.
InitializeComponent( );
}
● This is the file where event handler methods will be placed
22
23. InitializeComponent( ) Method
BackColor = Color.FromArgb (((Byte)(255)),
((Byte)(224)), ((Byte)(192)));
ClientSize = new Size(392, 373);
Font = new Font("Arial", 12F, FontStyle.Bold,
GraphicsUnit.Point, ((Byte)(0)));
ForeColor = Color.Blue;
Location = new Point(30, 30);
Margin = new Padding(4);
MaximizeBox = false;
Name = "Form1";
StartPosition =
FormStartPosition.CenterScreen;
Text = "First Windows Application";
● Some of the auto
generated code in the
method
○ Added as default values
for properties or from
changing property values
23
24. 24
Windows Form Events
● Add code to respond to events, like button clicks
○ Code goes into Form1.cs file
● From the Properties window, select the lightning bolt (Events)
○ Double-click on the event name to generate code
■ Registers the event as being of interest
■ Adds a heading for event-handler method
25. 25
Windows Form – Load Event
● Code automatically added to register event
● Code automatically added for method heading
private void Form1_Load(object sender, EventArgs e)
{
}
● You add statement to event-handler method body
this.BackColor = Color.Azure;
26. 26
Windows Form – FormClosing Event
● Code automatically added to register event
● Code automatically added for method heading
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
● You add statement to event-handler method body
MessageBox.Show("Hope you are having fun!");
27. 27
Controls
● Controls are all classes
○ Button, Label, TextBox, ComboBox, MainMenu, ListBox, CheckBox,
RadioButton, and MonthCalendar
● Each comes with its own predefined properties and methods
● Each fires events
● Each is derived from the System.Windows.Forms.Control class
30. 30
Controls (continued)
● Two procedures to place controls
○ From Toolbox, double-click on control or drag and drop
● Move, resize, and delete controls
● Format controls
○ Align controls
○ Make same size
○ Horizontal and vertical spacing
33. Methods of the Control class
● Control class has over 75 properties and over 100 methods
○ Not all are useful for every class that derives from it
33
Systems.Windows.Forms.Control methods
This table
includes a short
list of some of
the many
methods.
Explore MSDN
documentation
for more
34. Running the Windows Form Application
● To run the program you can use an F5 key or Play button present in the
toolbar of Visual Studio.
● To stop the program you can use pause button present in the ToolBar.
● You can also run the program by going to Debug->Start Debugging menu in
the menubar.
35. Practical Task
● Develop a Windows-based application enabling users to input their names.
Upon clicking a button, the application should showcase the entered name in
a dialog box.
36. Practical Task
● Create a Windows application that allows users to input their names. Upon
clicking a "Display" button, the application should present the entered name in
a label within the form. Clicking the "Clear" button should reset both the form
label and the text box.
37. Form Components Naming
● It is a good practice to change components, to track which component you
are currently working on.
● In the previous example, names could be lblFullName, txtFullName,
btnDisply, lblDisplayName and btnClear respectively.
39. 39
Creating a TaxApp Form
Add Label
objects to
Form
object…
Use
options on
FORMAT
menu
40. 40
Adding Labels to TaxApp Form
Add Label objects, then set
their properties using the
Properties window
(View Properties window)
41. Practical Task
Develop a simple calculator application that allowing users to perform basic
arithmetic operations such as addition, subtraction, multiplication, and division
42. GroupBox Component
● It is used to organize all form components in the same style.
● It can be used to set properties of all the components in the groupbox.
43. RadioButton, CheckBox and ComboBox
● Radio Button: Is used to select one of the available choices in a given
application.
● CheckBox: Is used to select none or all of the available options
● Combobox: Is used when you have a list of options to select from.
○ To prevent users from typing,change the DropDownStyle property to DropDownList
○ To type the options that you want to offer to your user, use the Items property list.
○ Combobox acts like an array, the first selectedIndex(0) indicates the first item in the list.
○ The selected item can be obtained by accessing the selectedItem property of the given
combo box.
44. Validation using ErrorProvider
● ErroProvider can be used to validate user inputs
private bool validation()
{
bool isValidated = true;
if (this.textName.Text=="")
{
isValidated = false;
errorProvider1.SetError(textName, "Please Enter Your Name:");
}
else
{
errorProvider1.Clear();
}
return isValidated;
}
45. Menus and Toolbars
● Navigation Menu:
○ Menustrip: A navigation menu.
○ You can use shortcuts
○ ContextMenu: Pop up menu
■ It has to be linked to a form component
○ ToolStrip: Similar to Menustrip. You can use components like label, buttons, etc