Ch
Ch
apter
apter
3
3
Programming with Windows Forms
Programming with Windows Forms
Department of Software Engineering
Department of Software Engineering
Faculty of Information Technology
Faculty of Information Technology
Natural Sciences University
Natural Sciences University
Agenda
Agenda
Agenda
Agenda
Introduction Windows Forms
Introduction Windows Forms
How to handle events in Windows Forms
How to handle events in Windows Forms
Adding controls to forms (design-time)
Adding controls to forms (design-time)
Dynamically adding controls to Forms
Dynamically adding controls to Forms
(runtime)
(runtime)
Using Complex Controls
Using Complex Controls
Creating GUI Components
Creating GUI Components
Working with Menu
Working with Menu
Creating MDI applications with Windows
Creating MDI applications with Windows
Forms
Forms
Deploying Windows Forms Applications
Deploying Windows Forms Applications
What is Windows Forms (a.k.a.
What is Windows Forms (a.k.a.
WinForms)?
WinForms)?
What is Windows Forms (a.k.a.
What is Windows Forms (a.k.a.
WinForms)?
WinForms)?
Windows Forms is part of the .NET
Windows Forms is part of the .NET
framework
framework
core classes in System.Windows.Forms
core classes in System.Windows.Forms
namespace
namespace
design-time support in various namespaces
design-time support in various namespaces
Windows Forms provides classes for
Windows Forms provides classes for
building UIs
building UIs
e.g. custom forms, common controls, standard
e.g. custom forms, common controls, standard
dialogs
dialogs
Visual Studio .NET provides tools for using
Visual Studio .NET provides tools for using
Windows Forms
Windows Forms
templates for common starting places, and a
templates for common starting places, and a
visual designer
visual designer
Windows Forms Application Structure
Windows Forms Application Structure
Windows Forms Application Structure
Windows Forms Application Structure
A Windows Forms application has three pieces
A Windows Forms application has three pieces
the application itself
the application itself
forms in the application
forms in the application
controls on the form
controls on the form
Application
mainForm
MyForm
label1
button1
Label
“Hell…”
Button
“OK”
System.Windows.Forms.Application
System.Windows.Forms.Application
System.Windows.Forms.Application
System.Windows.Forms.Application
The Application class represents the application itself
The Application class represents the application itself
no instances (all properties and methods are static)
no instances (all properties and methods are static)
processes UI events delivered by Windows
processes UI events delivered by Windows
Run
Run
,
,
DoEvents
DoEvents
provides access to application environment
provides access to application environment
ExecutablePath
ExecutablePath
,
,
StartupPath
StartupPath
CommonAppDataPath
CommonAppDataPath
,
,
UserAppDataPath
UserAppDataPath
CommonAppDataRegistry
CommonAppDataRegistry
,
,
UserAppDataRegistry
UserAppDataRegistry
class MyApp {
public static void Main() {
MyForm form = new MyForm();
System.Windows.Forms.Application.Run(form);
}
}
System.Windows.Forms.Form
System.Windows.Forms.Form
System.Windows.Forms.Form
System.Windows.Forms.Form
Instances of the Form class represent
Instances of the Form class represent
windows
windows
provide window-style services, e.g.
provide window-style services, e.g.
properties:
properties:
Text, Size, Location, Controls
Text, Size, Location, Controls
methods:
methods:
Show, ShowDialog, Close
Show, ShowDialog, Close
events:
events:
Load, Click, Closing
Load, Click, Closing
custom forms typically derive from base
custom forms typically derive from base
Form
Form
class
class
class MyForm : Form {
public MyForm() {
this.Text = "This is my form!";
this.Location = new Point(10, 10);
this.Size = new Size(100, 100);
}
}
Form appearance
Form appearance
Form appearance
Form appearance
Various properties influence a form’s appearance
Various properties influence a form’s appearance
Often, changing a property results in event notification
Often, changing a property results in event notification
Move
Move
(
(
Location
Location
),
),
Resize
Resize
(
(
Size
Size
),
),
FontChanged
FontChanged
(
(
Font
Font
)
)
Property Type Purpose
Text string Text to display (if applicable)
Location Point Upper/left coordinate of form
Size Point Width/height of form
Font Font Get/set displayed text font
ForeColor Color Get/set foreground color
Opacity double Get/set degree of opacity (as a
%)
(many more…) … …