spf chapter10 events
DESCRIPTION
C# .NET eventsTRANSCRIPT
![Page 1: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/1.jpg)
Chapter 10
C# .NET Events
![Page 2: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/2.jpg)
Button Click event Mouse Down event Key Down event Leave event
What will we be learning?
![Page 3: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/3.jpg)
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
![Page 4: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/4.jpg)
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
![Page 5: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/5.jpg)
private void button1_Click(object sender, EventArgs e)
{
….
}
Part 1 – The Click Event for Button
![Page 6: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/6.jpg)
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
![Page 7: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/7.jpg)
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
![Page 8: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/8.jpg)
VS IDE link the method as the event handler automatically
E.g. in Form1.Designer.cs
Part 1 – The Click Event for Button
![Page 9: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/9.jpg)
New solution with project: SpfChapter10_events
Add a new button, change text to "Quit" Add codes
To exit/quit an application
![Page 10: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/10.jpg)
Design View Select Form1
Part 2 – Mouse Down
![Page 11: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/11.jpg)
View Properties window Select the lightning bolt (events)
Part 2 – Mouse Down
![Page 12: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/12.jpg)
Double click on "MouseDown" event
Part 2 – Mouse Down
![Page 13: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/13.jpg)
We want to handle the event when a mouse click down on the form
Double click on "MouseDown" event
Part 2 – Mouse Down
![Page 14: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/14.jpg)
e.
MouseEventArgs e (Extra)
Left or Right button
Number of clicks
X, Y: position of mouse
![Page 15: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/15.jpg)
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
![Page 16: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/16.jpg)
e.
KeyEventArgs e (Extra)
Alt key is pressedCtrl key is pressed
Shft key is pressed
Data about the key pressed
![Page 17: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/17.jpg)
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
![Page 18: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/18.jpg)
Double click on "Leave" event
Part 4 – Leave event
![Page 19: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/19.jpg)
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
![Page 20: Spf chapter10 events](https://reader036.vdocuments.us/reader036/viewer/2022082915/546b31d0af795902048b5ab2/html5/thumbnails/20.jpg)
Button Click event Mouse Down event Key Down event Leave event
Summary