spf chapter10 events

Post on 18-Nov-2014

598 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

C# .NET events

TRANSCRIPT

Chapter 10

C# .NET Events

Button Click event Mouse Down event Key Down event Leave event

What will we be learning?

An Event is when something special happens

Inbuilt code gets activated when the Event happened

The Event is then said to be “handled” GUI Events such as when a button is

clicked, when the mouse right click and more

Event

An event can be a mouse click, a keystroke or an alarm

A class that raised the event is known as "event sender"

A method used to handle a particular event is known as "event handler"

In Visual Studio IDE, the mechanism is done automatically

private void button1_Click(object sender, EventArgs e)

{

….

}

Part 1 – The Click Event for Button

private void button1_Click(object sender, EventArgs e)

{

….

}

Part 1 – The Click Event for Button

Who is calling this method:in this case, it is button1

private void button1_Click(object sender, EventArgs e)

{

….

}

Part 1 – The Click Event for Button

What event is it?:in the next example, we shall use the event arguments to check if it is left or right mouse click

VS IDE link the method as the event handler automatically

E.g. in Form1.Designer.cs

Part 1 – The Click Event for Button

New solution with project: SpfChapter10_events

Add a new button, change text to "Quit" Add codes

To exit/quit an application

Design View Select Form1

Part 2 – Mouse Down

View Properties window Select the lightning bolt (events)

Part 2 – Mouse Down

Double click on "MouseDown" event

Part 2 – Mouse Down

We want to handle the event when a mouse click down on the form

Double click on "MouseDown" event

Part 2 – Mouse Down

e.

MouseEventArgs e (Extra)

Left or Right button

Number of clicks

X, Y: position of mouse

Design view, Select Form1 In Properties window, view events We want to handle when a key is pressed Double click on "KeyDown" event

Part 3 – Key Down

e.

KeyEventArgs e (Extra)

Alt key is pressedCtrl key is pressed

Shft key is pressed

Data about the key pressed

Design view, Add a new textbox In Properties window, view events We want to handle after user key in some

text and leaving the textbox

Part 4 – Leave event

Double click on "Leave" event

Part 4 – Leave event

The TabIndex property refers to which control will be selected when the Tab key is pressed

The TabIndex allows you to set the Tab order for all the controls on your form

TabIndex

Button Click event Mouse Down event Key Down event Leave event

Summary

top related