Upload
marjory-collins
View
213
Download
0
Embed Size (px)
Citation preview
C# H 1
CSC 298
Windows Forms
C# H 2
Programming Windows Forms Use the FCL classes in the namespace
System.Windows.Forms, e.g. Form: most Windows Forms applications derive from
Form Application: the Run method provides the message loop
to handle the interaction of the user with the Form Use also the classes in System.Drawing for graphics
capabilities (Graphics Device Interface+ GDI+), e.g. the Graphics class.
C# H 3
Inheritance hierarchy of Form
C# H 4
Using Forms Know about common structs
Color (create your color with FromArgb) Rectangle: upper left point (x,y), width, height Size: width and height Point: (x,y)
Know about the common form properties ForeColor, BackColor, Font, ClientRectangle,
Practice: see class website
C# H 5
Forms and events Input model for forms is event driven
any action on the form by the user (mouse click, key pressed, etc…) is associated with a different method in a class
the user's action triggers a call to the method To manage the user's actions, override these
methods in the class that inherits Form Common methods
OnMouseDown, OnMouseUp, OnMouseMove, OnPaint, etc…
C# H 6
Event Methods To override, write e.g. for OnPaint
protected override void OnPaint(PaintEventArgs pea) { /* your code */ }
All event methods take an argument that describe the event, for instance MouseEventArgs for mouse events KeyEventArgs, KeyPressEventArgs for keyboard
event EventArgs for events that don't carry a particular
description (resize event, change of the backcolor, etc…)
C# H 7
Alternative: create an event handler Create an event handler and attach it to the relevant
event, e.g. public class MyForm: Form{
{
public MyForm(){
PaintEventHandler myHandler = new PaintEventHandler(MyOnPaint);this.Paint += myHandler;
// rest of the constructor
}
private void MyOnPaint(Object source, PaintEventArgs pea) {/* code */}
}
C# H 8
Delegates Event handlers must match a method prototype called
the delegate, defined as for instancepublic delegate void PaintEventHandler(Object source, PaintEventArgs pea);
Common event handlers KeyEventHandler, MouseEventHandler, EventHandler
Common events on a form (look for ) mouse: Click, MouseMove, MouseUp, MouseDown,
etc… keyboard: KeyDown, KeyUp, KeyPress others: Resize, Closing, etc…
C# H 9
Controls on a form A form is a container. Thus it can have controls,
e.g. buttons, radio buttons, check boxes… Can't override the protected event methods for
these controls since their classes are not inherited
Must use the event handler approachpublic class MyForm: Form{private Button cmdOK = new Button();
public MyForm(){cmdOK.Click += new EventHandler(CmdOnClick);
} private void CmdOnClick(Object source, EventArgs ea){ /* code for click on OK button */ }
C# H 10
Defining your own events Rarely needed for common applications Create your own delegate to define the prototype of the
event handlerpublic delegate void MyEventHandler(Object source, MyEventArgs mea);
Create a variable of the type defined by the delegate: the eventpublic event MyEventHandler MyEvent;
The user of the class adds (+=) or removes (-=) event handlers to the event
See example (Phone.cs)