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

VB6_INTRODUCTION.ppt
VB6_INTRODUCTION.pptVB6_INTRODUCTION.ppt
VB6_INTRODUCTION.pptBhuvanaR13
 
vb-160518151614.pdf
vb-160518151614.pdfvb-160518151614.pdf
vb-160518151614.pdfLimEchYrr
 
vb-160518151614.pptx
vb-160518151614.pptxvb-160518151614.pptx
vb-160518151614.pptxLimEchYrr
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxAnasYunusa
 
Chapter 1
Chapter 1Chapter 1
Chapter 1gebrsh
 
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 classmnewg218
 
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 framworkDipen Parmar
 
Lect02 Introducing Programming.ppt
Lect02 Introducing Programming.pptLect02 Introducing Programming.ppt
Lect02 Introducing Programming.pptNourhanTarek23
 

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

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
 
Lect02 Introducing Programming.ppt
Lect02 Introducing Programming.pptLect02 Introducing Programming.ppt
Lect02 Introducing Programming.ppt
 

Recently uploaded

Tech Tuesday Slides - Getting Started with the Portfolio Module.
Tech Tuesday Slides - Getting Started with the Portfolio Module.Tech Tuesday Slides - Getting Started with the Portfolio Module.
Tech Tuesday Slides - Getting Started with the Portfolio Module.OnePlan Solutions
 
Reliable from-source builds (Qshare 28 Nov 2023).pdf
Reliable from-source builds (Qshare 28 Nov 2023).pdfReliable from-source builds (Qshare 28 Nov 2023).pdf
Reliable from-source builds (Qshare 28 Nov 2023).pdfRalf Gommers
 
OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
Preparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple ArchitecturesPreparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple ArchitecturesAke Koomsin
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
ManageIQ - Sprint 234 Review - Slide Deck
ManageIQ - Sprint 234 Review - Slide DeckManageIQ - Sprint 234 Review - Slide Deck
ManageIQ - Sprint 234 Review - Slide DeckManageIQ
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxRTS corp
 
AmsterdamJUG April 2024 - Going serverless with Quarkus GraalVM native images...
AmsterdamJUG April 2024 - Going serverless with Quarkus GraalVM native images...AmsterdamJUG April 2024 - Going serverless with Quarkus GraalVM native images...
AmsterdamJUG April 2024 - Going serverless with Quarkus GraalVM native images...Bert Jan Schrijver
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxAS Design & AST.
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdfSteve Caron
 
oracle 23c new features for developer and dba
oracle 23c new features for developer and dbaoracle 23c new features for developer and dba
oracle 23c new features for developer and dbaRemote DBA Services
 
logical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxlogical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxRemote DBA Services
 
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...Piyovi
 
full course of software engineering mid term.pdf
full course of software engineering mid term.pdffull course of software engineering mid term.pdf
full course of software engineering mid term.pdfAbdul salam
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...kalichargn70th171
 
Effort Estimation Techniques used in Software Projects
Effort Estimation Techniques used in Software ProjectsEffort Estimation Techniques used in Software Projects
Effort Estimation Techniques used in Software ProjectsDEEPRAJ PATHAK
 

Recently uploaded (20)

Tech Tuesday Slides - Getting Started with the Portfolio Module.
Tech Tuesday Slides - Getting Started with the Portfolio Module.Tech Tuesday Slides - Getting Started with the Portfolio Module.
Tech Tuesday Slides - Getting Started with the Portfolio Module.
 
Reliable from-source builds (Qshare 28 Nov 2023).pdf
Reliable from-source builds (Qshare 28 Nov 2023).pdfReliable from-source builds (Qshare 28 Nov 2023).pdf
Reliable from-source builds (Qshare 28 Nov 2023).pdf
 
OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
Preparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple ArchitecturesPreparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple Architectures
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
ManageIQ - Sprint 234 Review - Slide Deck
ManageIQ - Sprint 234 Review - Slide DeckManageIQ - Sprint 234 Review - Slide Deck
ManageIQ - Sprint 234 Review - Slide Deck
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptx
 
AmsterdamJUG April 2024 - Going serverless with Quarkus GraalVM native images...
AmsterdamJUG April 2024 - Going serverless with Quarkus GraalVM native images...AmsterdamJUG April 2024 - Going serverless with Quarkus GraalVM native images...
AmsterdamJUG April 2024 - Going serverless with Quarkus GraalVM native images...
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptx
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
 
oracle 23c new features for developer and dba
oracle 23c new features for developer and dbaoracle 23c new features for developer and dba
oracle 23c new features for developer and dba
 
logical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxlogical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptx
 
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
 
full course of software engineering mid term.pdf
full course of software engineering mid term.pdffull course of software engineering mid term.pdf
full course of software engineering mid term.pdf
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
 
Effort Estimation Techniques used in Software Projects
Effort Estimation Techniques used in Software ProjectsEffort Estimation Techniques used in Software Projects
Effort Estimation Techniques used in Software Projects
 

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