SlideShare a Scribd company logo
1 of 45
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

Similar to LECTURE 12 WINDOWS FORMS PART 2.pptx

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

Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
 
Visual studio.net
Visual studio.netVisual studio.net
Visual studio.net
 
VB6_INTRODUCTION.ppt
VB6_INTRODUCTION.pptVB6_INTRODUCTION.ppt
VB6_INTRODUCTION.ppt
 
vb.pptx
vb.pptxvb.pptx
vb.pptx
 
vb-160518151614.pdf
vb-160518151614.pdfvb-160518151614.pdf
vb-160518151614.pdf
 
vb-160518151614.pptx
vb-160518151614.pptxvb-160518151614.pptx
vb-160518151614.pptx
 
4.C#
4.C#4.C#
4.C#
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptx
 
Intake 37 9
Intake 37 9Intake 37 9
Intake 37 9
 
Vb.net ide
Vb.net ideVb.net ide
Vb.net ide
 
Ch01
Ch01Ch01
Ch01
 
Chapter 01
Chapter 01Chapter 01
Chapter 01
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Visual programming basic.ppt bs cs5th class
Visual programming basic.ppt bs cs5th classVisual programming basic.ppt bs cs5th class
Visual programming basic.ppt bs cs5th class
 
Visual studio ide componects dot net framwork
Visual studio ide componects dot net framworkVisual studio ide componects dot net framwork
Visual studio ide componects dot net framwork
 
Intake 38 9
Intake 38 9Intake 38 9
Intake 38 9
 
Visual basic
Visual basicVisual basic
Visual basic
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
 
unit 4.docx
unit 4.docxunit 4.docx
unit 4.docx
 
Visual basic
Visual basicVisual basic
Visual basic
 

Recently uploaded

[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypseTomasz Kowalczewski
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Andrea Goulet
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...drm1699
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Conceptsthomashtkim
 
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...Neo4j
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Andreas Granig
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In sowetokasambamuno
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfkalichargn70th171
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Lisi Hocke
 
What is a Recruitment Management Software?
What is a Recruitment Management Software?What is a Recruitment Management Software?
What is a Recruitment Management Software?NYGGS Automation Suite
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio, Inc.
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024SimonedeGijt
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbankkasambamuno
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Flutter Agency
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationElement34
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAShane Coughlan
 

Recently uploaded (20)

[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Concepts
 
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
What is a Recruitment Management Software?
What is a Recruitment Management Software?What is a Recruitment Management Software?
What is a Recruitment Management Software?
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test Automation
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 

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