SlideShare a Scribd company logo
CS 2104: Visual
Programming
Lecture 4:Introduction to C# Windows Forms
Applications
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
Contrasting Windows and Console Applications
● Windows applications function differently from console applications.
● Windows applications look differently from console applications.
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
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
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
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
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.
Use Visual Studio to Create Windows-Based Applications
Use Visual Studio to Create Windows-Based Applications
Project Windows
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
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
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.
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.
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.
Windows Form
18
Windows Form Properties
Properties
Property value
Windows Form Events
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
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
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
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
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
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
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
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
28
Controls (continued)
29
Standard Controls
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
31
Properties of the Control class
Properties of the Control class (continued)
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
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.
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.
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.
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.
38
Creating a TaxApp
Properties set for the
Form container
Table 9-4 TaxApp Form1 properties
39
Creating a TaxApp Form
Add Label
objects to
Form
object…
Use
options on
FORMAT
menu
40
Adding Labels to TaxApp Form
Add Label objects, then set
their properties using the
Properties window
(View Properties window)
Practical Task
Develop a simple calculator application that allowing users to perform basic
arithmetic operations such as addition, subtraction, multiplication, and division
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.
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.
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;
}
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

More Related Content

PPTX
Windows form application - C# Training
Moutasm Tamimi
 
PDF
Integer programming
Hakeem-Ur- Rehman
 
PPTX
Information Privacy
imehreenx
 
PPTX
Windowforms controls c#
prabhu rajendran
 
PPT
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
PPTX
Regular Expression Examples.pptx
GhulamRabani9
 
PPT
Information security
razendar79
 
PPTX
C# 101: Intro to Programming with C#
Hawkman Academy
 
Windows form application - C# Training
Moutasm Tamimi
 
Integer programming
Hakeem-Ur- Rehman
 
Information Privacy
imehreenx
 
Windowforms controls c#
prabhu rajendran
 
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
Regular Expression Examples.pptx
GhulamRabani9
 
Information security
razendar79
 
C# 101: Intro to Programming with C#
Hawkman Academy
 

What's hot (20)

PPTX
Array in c#
Prem Kumar Badri
 
PPT
ADO .Net
DrSonali Vyas
 
PPT
Visual Basic menu
kuldeep94
 
PPTX
Java(Polymorphism)
harsh kothari
 
PPTX
Event handling
swapnac12
 
PPT
Java interfaces
Raja Sekhar
 
PPT
Assembler
manpreetgrewal
 
PPT
File Handling In C++(OOPs))
Papu Kumar
 
PPTX
CSharp Presentation
Vishwa Mohan
 
PPTX
JAVA AWT
shanmuga rajan
 
PPS
Procedures functions structures in VB.Net
tjunicornfx
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPTX
interface in c#
Deepti Pillai
 
PPTX
User defined functions
Rokonuzzaman Rony
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PPTX
Constructor in java
Madishetty Prathibha
 
PPTX
C# Tutorial
Jm Ramos
 
PPTX
Inline function
Tech_MX
 
PPTX
Java awt (abstract window toolkit)
Elizabeth alexander
 
PPTX
Sdi & mdi
BABAVALI S
 
Array in c#
Prem Kumar Badri
 
ADO .Net
DrSonali Vyas
 
Visual Basic menu
kuldeep94
 
Java(Polymorphism)
harsh kothari
 
Event handling
swapnac12
 
Java interfaces
Raja Sekhar
 
Assembler
manpreetgrewal
 
File Handling In C++(OOPs))
Papu Kumar
 
CSharp Presentation
Vishwa Mohan
 
JAVA AWT
shanmuga rajan
 
Procedures functions structures in VB.Net
tjunicornfx
 
Classes, objects in JAVA
Abhilash Nair
 
interface in c#
Deepti Pillai
 
User defined functions
Rokonuzzaman Rony
 
Function C programming
Appili Vamsi Krishna
 
Constructor in java
Madishetty Prathibha
 
C# Tutorial
Jm Ramos
 
Inline function
Tech_MX
 
Java awt (abstract window toolkit)
Elizabeth alexander
 
Sdi & mdi
BABAVALI S
 
Ad

Similar to LECTURE 12 WINDOWS FORMS PART 2.pptx (20)

PPTX
CHAPTER 1: Lesson B
MLG College of Learning, Inc
 
PPTX
hjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptx
EliasPetros
 
PPTX
Spf chapter 03 WinForm
Hock Leng PUAH
 
PPT
Meaning Of VB
Mohit Verma
 
PPT
06 win forms
mrjw
 
PDF
Visual basic 6.0
sanket1996
 
PPTX
SPF WinForm Programs
Hock Leng PUAH
 
PPT
Csc153 chapter 03
PCC
 
PPTX
Vb6.0 intro
JOSEPHINEA6
 
PPT
Visual studio.net
Dr. C.V. Suresh Babu
 
PPT
Windows form applicationWindows form application
juraevmaruff
 
PPT
VB6_INTRODUCTION.ppt
BhuvanaR13
 
PPTX
vb.pptx
CherryLim21
 
PDF
vb-160518151614.pdf
LimEchYrr
 
PPTX
vb-160518151614.pptx
LimEchYrr
 
PPT
4.C#
Raghu nath
 
PPTX
COM 211 PRESENTATION.pptx
AnasYunusa
 
PDF
Intake 37 9
Mahmoud Ouf
 
PPTX
Vb.net ide
Faisal Aziz
 
PPT
Ch01
guest7a6cbb3
 
CHAPTER 1: Lesson B
MLG College of Learning, Inc
 
hjksjdhksjhcksjhckjhskdjhcskjhckjdppt.pptx
EliasPetros
 
Spf chapter 03 WinForm
Hock Leng PUAH
 
Meaning Of VB
Mohit Verma
 
06 win forms
mrjw
 
Visual basic 6.0
sanket1996
 
SPF WinForm Programs
Hock Leng PUAH
 
Csc153 chapter 03
PCC
 
Vb6.0 intro
JOSEPHINEA6
 
Visual studio.net
Dr. C.V. Suresh Babu
 
Windows form applicationWindows form application
juraevmaruff
 
VB6_INTRODUCTION.ppt
BhuvanaR13
 
vb.pptx
CherryLim21
 
vb-160518151614.pdf
LimEchYrr
 
vb-160518151614.pptx
LimEchYrr
 
COM 211 PRESENTATION.pptx
AnasYunusa
 
Intake 37 9
Mahmoud Ouf
 
Vb.net ide
Faisal Aziz
 
Ad

More from AOmaAli (16)

PPTX
Introduction to Foundation of Computing Lecture 1.pptx
AOmaAli
 
PPTX
Lecture 3 Introduction to HTML FORM AND CSS.pptx
AOmaAli
 
PPTX
Lecture 9 - Intruduction to BOOTSTRAP.pptx
AOmaAli
 
PPTX
Lecture 6: Introduction to PHP and Mysql .pptx
AOmaAli
 
PPTX
lecture 7 - Introduction to MySQL with PHP.pptx
AOmaAli
 
PPT
3-itec229 sddds dfsddfdf dfdfdf _chapter1.ppt
AOmaAli
 
PDF
Instructional Design - Storyboard_IT Teaching Methods_ED 2216_MSM.pdf
AOmaAli
 
PPTX
SSdsdc dssdsd sfdddsd sdsds assas sdsddsdsdAD.pptx
AOmaAli
 
PPTX
LECTURE 1-BASIC CONCEPT OF INFORMATION SYSTEM.pptx
AOmaAli
 
PPTX
Lecture 2 Software Development Process and SDCL models.pptx
AOmaAli
 
PPTX
LECTURE 2 -Object oriented Java Basics.pptx
AOmaAli
 
PPTX
LECTURE 1-BASIC CONCEPT OF INFORMATION SYSTEM.pptx
AOmaAli
 
PPTX
Software development life cycle (SDLC) Models
AOmaAli
 
PPTX
LECTURE 14 Data Access.pptx
AOmaAli
 
PPTX
LECTURE 1 - Introduction to Programming.pptx
AOmaAli
 
PPTX
LECTURE 1-Introduction to mobile communication systems.pptx
AOmaAli
 
Introduction to Foundation of Computing Lecture 1.pptx
AOmaAli
 
Lecture 3 Introduction to HTML FORM AND CSS.pptx
AOmaAli
 
Lecture 9 - Intruduction to BOOTSTRAP.pptx
AOmaAli
 
Lecture 6: Introduction to PHP and Mysql .pptx
AOmaAli
 
lecture 7 - Introduction to MySQL with PHP.pptx
AOmaAli
 
3-itec229 sddds dfsddfdf dfdfdf _chapter1.ppt
AOmaAli
 
Instructional Design - Storyboard_IT Teaching Methods_ED 2216_MSM.pdf
AOmaAli
 
SSdsdc dssdsd sfdddsd sdsds assas sdsddsdsdAD.pptx
AOmaAli
 
LECTURE 1-BASIC CONCEPT OF INFORMATION SYSTEM.pptx
AOmaAli
 
Lecture 2 Software Development Process and SDCL models.pptx
AOmaAli
 
LECTURE 2 -Object oriented Java Basics.pptx
AOmaAli
 
LECTURE 1-BASIC CONCEPT OF INFORMATION SYSTEM.pptx
AOmaAli
 
Software development life cycle (SDLC) Models
AOmaAli
 
LECTURE 14 Data Access.pptx
AOmaAli
 
LECTURE 1 - Introduction to Programming.pptx
AOmaAli
 
LECTURE 1-Introduction to mobile communication systems.pptx
AOmaAli
 

Recently uploaded (20)

PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PDF
Immersive experiences: what Pharo users do!
ESUG
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Presentation about variables and constant.pptx
safalsingh810
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Exploring AI Agents in Process Industries
amoreira6
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
Immersive experiences: what Pharo users do!
ESUG
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 

LECTURE 12 WINDOWS FORMS PART 2.pptx

  • 1. CS 2104: Visual Programming Lecture 4:Introduction to C# Windows Forms Applications
  • 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.
  • 9. Use Visual Studio to Create Windows-Based Applications
  • 10. Use Visual Studio to Create Windows-Based Applications
  • 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
  • 31. 31 Properties of the Control class
  • 32. Properties of the Control class (continued)
  • 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.
  • 38. 38 Creating a TaxApp Properties set for the Form container Table 9-4 TaxApp Form1 properties
  • 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