Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 298 Windows Forms.

Similar presentations


Presentation on theme: "CSC 298 Windows Forms."— Presentation transcript:

1 CSC 298 Windows Forms

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.

3 Inheritance hierarchy of Form

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

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…

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…)

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

8 Delegates Event handlers must match a method prototype called the delegate, defined as for instance public 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…

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 approach public 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 */ }

10 Defining your own events
Rarely needed for common applications Create your own delegate to define the prototype of the event handler public delegate void MyEventHandler(Object source, MyEventArgs mea); Create a variable of the type defined by the delegate: the event public event MyEventHandler MyEvent; The user of the class adds (+=) or removes (-=) event handlers to the event See example (Phone.cs)


Download ppt "CSC 298 Windows Forms."

Similar presentations


Ads by Google