Ch
Ch
apter
apter3
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
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
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
Windows Forms Application
Structure
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”
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
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:
),
Resize
Resize
(
(
Size
Size
),
),
FontChanged
FontChanged
(
(
Font
Font
)
)
Property
Property
Type
Type
Purpose
Purpose
Text
Text
string
string
Text to display (if applicable)
Text to display (if applicable)
Location
a %)
(many more…)
(many more…)
…
…
…
…
Controls
Controls
Controls are visual components
Controls are visual components
System.Windows.Forms.Control is base class for UI
System.Windows.Forms.Control is base class for UI
elements
elements
e.g.
e.g.
Form, Button, Label, TextBox, ListBox
Form, Button, Label, TextBox, ListBox
, etc.
, etc.
contained and arranged by parent (usually a Form)
contained and arranged by parent (usually a Form)
held in parent’s
held in parent’s
Controls
Controls
collection
collection
public class MyForm : Form
{
// Handle event as needed
}
}
Control interaction (part 2)
Control interaction (part 2)
Derived controls/forms have two approaches
Derived controls/forms have two approaches
may register for base class events
may register for base class events
may override corresponding virtual functions (if provided)
may override corresponding virtual functions (if provided)
Decision driven by functionality desired, not
Decision driven by functionality desired, not
performance
performance
event versus override approach equivalent in many cases
event versus override approach equivalent in many cases
overriding provides control over when/if events are fired
overriding provides control over when/if events are fired
public EveryOtherClickButton : Button
{
private int clickNum = 0;
protected override void OnClick( EventArgs e )
{
clickNum++;
if( (clickNum % 2) == 0 )
base.OnClick(e); // Button.OnClick fires Click event
}
}
controls
controls
Controls for Navigating Data
Controls for Navigating Data
The Explorer Interface
The Explorer Interface
User Controls
User Controls
A
A
User Control
User Control
has all the basic functionality
has all the basic functionality
for a graphical control that will be used on a
for a graphical control that will be used on a
Windows Form.
Windows Form.
Inherits from
Inherits from
System.Windows.Forms.UserControl
System.Windows.Forms.UserControl
Inherited Properties
Inherited Properties
can override these properties.
can override these properties.
Inherited Events
Inherited Events
can override these inherited events if you need
can override these inherited events if you need
is used to provide users with an easily
is used to provide users with an easily
accessible
accessible
Pop-up
Pop-up
menus that appear when you
menus that appear when you
right-click a form or control.
right-click a form or control.
MDI applications
MDI applications
MDI – Multiple Document Interface
MDI – Multiple Document Interface
Form Properties
Form Properties
IsMDIContainer
IsMDIContainer
MDIParent
MDIParent
ActiveMDIChild
ActiveMDIChild
MenuItem Properties
MenuItem Properties
MergeType & MergeOrder
MergeType & MergeOrder
MDIList
MDIList
Arrange MDIChilds
Arrange MDIChilds
package based on the
package based on the
state of the machine
state of the machine
allows to roll back
allows to roll back
installations.
installations.
Deployment Projects
Deployment Projects
Templates
Templates
Deploying Windows Forms
Deploying Windows Forms
Applications (cont)
Applications (cont)
Creating a Windows Installer Package
Creating a Windows Installer Package
Setup project properties
Setup project properties
File Installation Management
File Installation Management
Registry Settings Management
Registry Settings Management
File Types Management
File Types Management
User Interface Management
User Interface Management
Custom Actions Management
Custom Actions Management
Directory: dotNET
Directory: dotNET