Tài liệu Windows Forms
Trang 1Chapter 3
Programming with Windows Forms Chapter 3
Programming with Windows Forms
Department of Software Engineering Faculty of Information Technology
Natural Sciences University
Trang 2Introduction Windows Forms
How to handle events in Windows Forms Adding controls to forms (design-time) Dynamically adding controls to Forms (runtime)
Using Complex Controls
Creating GUI Components
Working with Menu
Creating MDI applications with Windows Forms
Deploying Windows Forms Applications
Trang 3What is Windows Forms (a.k.a
WinForms)?
What is Windows Forms (a.k.a
WinForms)?
Windows Forms is part of the NET
framework
core classes in System.Windows.Forms
namespace
design-time support in various namespaces
Windows Forms provides classes for
building UIs
e.g custom forms, common controls, standard dialogs
Visual Studio NET provides tools for using Windows Forms
templates for common starting places, and a visual designer
Trang 4Windows Forms Application
Structure
Windows Forms Application
Structure
A Windows Forms application has three pieces
the application itself
forms in the application
controls on the form
Application
mainForm
MyForm
label1 button1
Label
“Hell…”
Button
“OK”
Trang 5The Application class represents the application
itself
no instances (all properties and methods are static)
processes UI events delivered by Windows
Run, DoEvents
provides access to application environment
ExecutablePath, StartupPath
CommonAppDataPath, UserAppDataPath
CommonAppDataRegistry, UserAppDataRegistry
class MyApp {
public static void Main() {
MyForm form = new MyForm();
System.Windows.Forms.Application.Run(form);
}
}
Trang 6Instances of the Form class represent
windows
provide window-style services, e.g.
custom forms typically derive from base Form
class
class MyForm : Form { public MyForm() { this.Text = "This is my form!";
this.Location = new Point(10, 10);
this.Size = new Size(100, 100);
} }
Trang 7Form appearance
Various properties influence a form’s appearance
Often, changing a property results in event notification
Property Type Purpose
Trang 8Controls are visual components
System.Windows.Forms.Control is base class for UI elements
e.g Form, Button, Label, TextBox, ListBox, etc.
contained and arranged by parent (usually a Form)
held in parent’s Controls 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);
} }
Trang 9Control interaction (part 1)
Public events are used by control
“consumers”
containing form/control registers for events of interest
public class MyForm : Form
{
private Button button1;
public MyForm()
{
button1 = new Button();
button1.Click += new EventHandler(button1_Click);
{
// Handle event as needed
}
}
Trang 10Control interaction (part 2)
Derived controls/forms have two approaches
may register for base class events
may override corresponding virtual functions (if provided)
Decision driven by functionality desired, not
performance
event versus override approach equivalent in many cases overriding provides control over when/if events are fired
public EveryOtherClickButton : Button
{
private int clickNum = 0;
clickNum++;
if( (clickNum % 2) == 0 )
}
}
Trang 11Complex Controls
Complex Controls
Docking Controls
Anchor a control to
one edge of its container
Make a control fill
the available space in its container
Splitter Windows
Allow docked controls to be resized at run time
by the user
Panels Control
Group controls together or subdivide a form into functional areas
Controls for Navigating Data
Trang 12The Explorer Interface
Trang 13User Controls
for a graphical control that will be used on a Windows Form
Inherits from
System.Windows.Forms.UserControl
Inherited Properties
can override these properties.
Inherited Events
can override these inherited events if you need
to, but it is best if you override only the inherited
overriding the base event, so that future controls may benefit from the standards.
Trang 14displays a menu at run time.
All submenus of the main menu and individual items are MenuItem objects.
is used to provide users with an easily
accessible Pop-up menus that appear when you right-click a form or control
Trang 15MDI applications
MDI – Multiple Document Interface
Form Properties
IsMDIContainer
MDIParent
ActiveMDIChild
MenuItem Properties
MergeType & MergeOrder
MDIList
Arrange MDIChilds
MdiLayout.TileVertical
Trang 16Deploying Windows Forms
Applications
Deploying Windows Forms
Applications
Microsoft Windows
Installer Service
keeps track of every
application that's
installed on a computer
allows to uninstall,
repair, or reinstall a
package based on the
state of the machine
allows to roll back
installations
Deployment Projects
Templates
Trang 17Deploying Windows Forms
Applications (cont)
Deploying Windows Forms
Applications (cont)
Creating a Windows Installer Package
Setup project properties
File Installation Management
Registry Settings Management
File Types Management
User Interface Management
Custom Actions Management
Launch Condition Management
Trang 18www.msdn.microsoft.com
MS Press Microsoft Visual C Sharp Dot NET Step By Step Version.2003 - l-mcs301-2003-11-25
Sams Teach Yourself
Visual.Studio.Dot.Net.2003.In.21Days -
l-stdn02-2003-7-11.rar
Directory: dotNET