3-WindowsForms

- 0 / 0
(Tài liệu chưa được thẩm định)
Nguồn: st
Người gửi: Trần Đức Anh (trang riêng)
Ngày gửi: 09h:01' 07-11-2011
Dung lượng: 1.2 MB
Số lượt tải: 2
Nguồn: st
Người gửi: Trần Đức Anh (trang riêng)
Ngày gửi: 09h:01' 07-11-2011
Dung lượng: 1.2 MB
Số lượt tải: 2
Số lượt thích:
0 người
Chapter 3
Programming with Windows Forms
Department of Software Engineering
Faculty of Information Technology
Natural Sciences University
Agenda
Introduction 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
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
Windows Forms Application Structure
A Windows Forms application has three pieces
the application itself
forms in the application
controls on the form
System.Windows.Forms.Application
The 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);
}
}
System.Windows.Forms.Form
Instances of the Form class represent windows
provide window-style services, e.g.
properties: Text, Size, Location, Controls
methods: Show, ShowDialog, Close
events: Load, Click, Closing
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);
}
}
Form appearance
Various properties influence a form’s appearance
Often, changing a property results in event notification
Move (Location), Resize (Size), FontChanged (Font)
Controls
Controls 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);
}
}
Control 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);
}
void button1_Click( object sender, EventArgs e )
{
// Handle event as needed...
}
}
Control 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;
protected override void OnClick( EventArgs e )
{
clickNum++;
if( (clickNum % 2) == 0 )
base.OnClick(e); // Button.OnClick fires Click event
}
}
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
TreeView and ListView controls
Controls for Navigating Data
The Explorer Interface
User Controls
A User Control has all the basic functionality 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 On method instead of directly overriding the base event, so that future controls may benefit from the standards.
Menu
MainMenu component
displays a menu at run time.
All submenus of the main menu and individual items are MenuItem objects.
ContextMenu component
is used to provide users with an easily accessible Pop-up menus that appear when you right-click a form or control.
MDI applications
MDI – Multiple Document Interface
Form Properties
IsMDIContainer
MDIParent
ActiveMDIChild
MenuItem Properties
MergeType & MergeOrder
MDIList
Arrange MDIChilds
MdiLayout.Cascade, MdiLayout.TileHorizontal, or MdiLayout.TileVertical
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
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
References
www.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
FTP: 172.29.22.45
Username: sv
Password: sv
Directory: dotNET
Programming with Windows Forms
Department of Software Engineering
Faculty of Information Technology
Natural Sciences University
Agenda
Introduction 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
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
Windows Forms Application Structure
A Windows Forms application has three pieces
the application itself
forms in the application
controls on the form
System.Windows.Forms.Application
The 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);
}
}
System.Windows.Forms.Form
Instances of the Form class represent windows
provide window-style services, e.g.
properties: Text, Size, Location, Controls
methods: Show, ShowDialog, Close
events: Load, Click, Closing
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);
}
}
Form appearance
Various properties influence a form’s appearance
Often, changing a property results in event notification
Move (Location), Resize (Size), FontChanged (Font)
Controls
Controls 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);
}
}
Control 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);
}
void button1_Click( object sender, EventArgs e )
{
// Handle event as needed...
}
}
Control 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;
protected override void OnClick( EventArgs e )
{
clickNum++;
if( (clickNum % 2) == 0 )
base.OnClick(e); // Button.OnClick fires Click event
}
}
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
TreeView and ListView controls
Controls for Navigating Data
The Explorer Interface
User Controls
A User Control has all the basic functionality 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 On
Menu
MainMenu component
displays a menu at run time.
All submenus of the main menu and individual items are MenuItem objects.
ContextMenu component
is used to provide users with an easily accessible Pop-up menus that appear when you right-click a form or control.
MDI applications
MDI – Multiple Document Interface
Form Properties
IsMDIContainer
MDIParent
ActiveMDIChild
MenuItem Properties
MergeType & MergeOrder
MDIList
Arrange MDIChilds
MdiLayout.Cascade, MdiLayout.TileHorizontal, or MdiLayout.TileVertical
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
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
References
www.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
FTP: 172.29.22.45
Username: sv
Password: sv
Directory: dotNET
 
Nói với em
| Nếu nhắm mắt trong vườn lộng gió, | |
| Sẽ được nghe nhiều tiếng chim hay, | |
| Tiếng lích rích chim sâu trong lá, | |
| Con chìa vôi vừa hót vừa bay. | |
| Nếu nhắm mắt nghe bà kể chuyện, | |
| Sẽ được nhìn thấy các bà tiên, | |
| Thấy chú bé đi hài bảy dặm, | |
| Quả thị thơm, cô Tấm rất hiền. | |
| Nếu nhắm mắt nghĩ về cha mẹ, | |
| Đã nuôi em khôn lớn từng ngày, | |
| Tay bồng bế sớm khuya vất vả, | |
| Mắt nhắm rồi lại mở ra ngay. | |
Thơ: Vũ Quần Phương |
|
| Tuyển tập ca
khúc thiếu nhi 2010, trang 202 Trung tâm văn hoá - nhà thiếu nhi TP.HCM |












Các ý kiến mới nhất