Tải bản đầy đủ (.ppt) (18 trang)

Chapter 3: Programming with Windows Forms pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (428.71 KB, 18 trang )

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
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)?
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
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”

System.Windows.Forms.Application
System.Windows.Forms.Application
The Application class represents the application
The Application class represents the application
itself
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
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
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
Property
Type
Type
Purpose
Purpose
Text
Text
string
string
Text to display (if applicable)
Text to display (if applicable)
Location

Location
Point
Point
Upper/left coordinate of form
Upper/left coordinate of form
Size
Size
Point
Point
Width/height of form
Width/height of form
Font
Font
Font
Font
Get/set displayed text font
Get/set displayed text font
ForeColor
ForeColor
Color
Color
Get/set foreground color
Get/set foreground color
Opacity
Opacity
double
double
Get/set degree of opacity (as
Get/set degree of opacity (as
a %)

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

{
private Button button1;
public MyForm()
{
button1 = new Button();
button1.Text = "Click Me!";
button1.Location = new Point(10, 10);
this.Controls.Add(button1);
}
}
Control interaction (part 1)
Control interaction (part 1)
Public events are used by control
Public events are used by control
“consumers”
“consumers”
containing form/control registers for events of
containing form/control registers for events of
interest
interest
public class MyForm : Form
{
private Button button1;
public MyForm()
{
button1 = new Button();

button1.Click += new EventHandler(button1_Click);
}
void button1_Click( object sender, EventArgs e )

{
// 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
}
}

Complex Controls
Complex Controls
Docking Controls
Docking Controls
Anchor a control to
Anchor a control to
one edge of its container
one edge of its container
Make a control fill
Make a control fill
the available space in its container
the available space in its container
Splitter Windows
Splitter Windows
Allow docked controls to be resized at run time
Allow docked controls to be resized at run time
by the user
by the user
Panels Control
Panels Control
Group controls together or subdivide a form into
Group controls together or subdivide a form into
functional areas
functional areas
TreeView
TreeView
and
and
ListView
ListView

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

to, but it is best if you override only the inherited
to, but it is best if you override only the inherited
On<EventName>
On<EventName>
method instead of directly
method instead of directly
overriding the base event, so that future controls
overriding the base event, so that future controls
may benefit from the standards.
may benefit from the standards.
Menu
Menu
MainMenu
MainMenu
component
component
displays a menu at run time.
displays a menu at run time.
All submenus of the main menu and individual
All submenus of the main menu and individual
items are
items are
MenuItem
MenuItem
objects.
objects.
ContextMenu
ContextMenu
component
component

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

MdiLayout.Cascade
MdiLayout.Cascade
,
,
MdiLayout.TileHorizontal
MdiLayout.TileHorizontal
, or
, or
MdiLayout.TileVertical
MdiLayout.TileVertical


Deploying Windows Forms
Deploying Windows Forms
Applications
Applications
Microsoft Windows
Microsoft Windows
Installer Service
Installer Service
keeps track of every
keeps track of every
application that's
application that's
installed on a computer
installed on a computer
allows to uninstall,
allows to uninstall,
repair, or reinstall a
repair, or reinstall a

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

Launch Condition Management
Launch Condition Management
References
References
www.msdn.microsoft.com
www.msdn.microsoft.com
MS Press Microsoft Visual C Sharp Dot NET
MS Press Microsoft Visual C Sharp Dot NET
Step By Step Version.2003 - l-mcs301-2003-
Step By Step Version.2003 - l-mcs301-2003-
11-25
11-25
Sams Teach Yourself
Sams Teach Yourself
Visual.Studio.Dot.Net.2003.In.21Days - l-
Visual.Studio.Dot.Net.2003.In.21Days - l-
stdn02-2003-7-11.rar
stdn02-2003-7-11.rar
FTP:
FTP:
172.29.22.45
172.29.22.45
Username:
Username:
sv
sv
Password:
Password:
sv
sv

Directory: dotNET
Directory: dotNET

×