36
Events Events

Events. Events Single Event Handlers Click Event Mouse Events Key Board Events Create and handle controls in runtime Outline

  • View
    288

  • Download
    0

Embed Size (px)

Citation preview

EventsEvents

•Events

•Single Event Handlers

•Click Event

•Mouse Events

•Key Board Events

•Create and handle controls in runtime

OutlineOutline

Events• An event is something that happens. Your birthday is

an event. • An event in programming terminology is when

something special happens. These events are so special that they are built in to the programming language.

• VB.NET has numerous Events that you can write code for. And we’re going to explore some of them in this course.

• We’ll start with all that mysterious code for the Button’s Click Event.

The Click Event• Buttons have the ability to be clicked on. • When you click a button, the event that is fired is the Click

Event. • If you were to add a new button to a form, and then double

clicked it, you would see the following code stub:

Private Sub btnOutput_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnOutput.Click

End Sub

• For example suppose we want the Button btnOutput to respond when clicked by showing a message box with the statement Button was clicked.

Private Sub btnOutput_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnOutput.Click

MessageBox.Show(“Button was clicked.")End Sub

• An event is a message sent by an object announcing that something has happened.

• When events occurs, information is passed to Event handlers ( Here it is btnOutput.Click).

The Click Event

• This is a Private Subroutine. • The name of the Sub is btnOutput_Click. • The Event itself is at the end: btnOutput.Click.• The Handles word means that this Subroutine can

Handle the Click Event of btnOutput.• You can have this btnOutput_Click Sub Handle other

things, too. • It can Handle the Click Event of other Buttons, for

example. Handles btnOutput.Click, Button2.Click

The Click Event

Event handlers take two arguments:1. An Object (usually sender) :

Instead of sender being an integer or string variable, the type of variable set up for sender is System.Object. This stores a reference to a control (which button was clicked, for example).

2. An event arguments object (e) : An instance of type EventArgs. Class EventArgs is the base class for objects that contain event information.

Event Handler Arguments

e argument :

Contain information you need to process the event. The information available depends on the type of event that was raised

Event Handler arguments – e argument

Mouse-Event Handling

• This section explains the handling of mouse events, such as clicks, presses and moves, which are generated when the mouse interacts with a control.

• Mouse event information is passed through class MouseEventArgs.

• Each mouse-event-handling method requires an Object (sender) and a MouseEventArgs object (e) as arguments.

• Class MouseEventArgs contains information related to the mouse event, such as the x- and y-coordinates of the mouse pointer, the mouse button pressed Right, Left or Middle), the number of times the mouse was clicked

• Note that the x- and y-coordinates of the MouseEventArgs object are relative to the control that generated the event. Point (0,0) represents the

upper-left corner of the control.• The X property returns how far across, from left to

right, the mouse is; the Y property returns how far down, from top to bottom, the mouse is.

Mouse-Event Handling

Mouse-Event Handling

1 ' Fig. 12.33: Painter.vb2 ' Using the mouse to draw on a form.3 4 Public Class FrmPainter56 7 Dim shouldPaint As Boolean = False8 9 ' Visual Studio .NET IDE generated code10 11 ' draw circle if shouldPaint is True12 Private Sub FrmPainter_MouseMove( _13 ByVal sender As System.Object, _14 ByVal e As System.Windows.Forms.MouseEventArgs) _15 Handles Me.MouseMove16 17 ' paint circle if mouse pressed18 If shouldPaint Then19 Dim graphic As Graphics = CreateGraphics()20 21 graphic.FillEllipse _22 (New SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4)23 End If24 25 End Sub ' FrmPainter_MouseMove

We want the program to draw only while the mouse button is pressed (i.e.,held down).

Graphics object offers methods that draw various shapes. For example, method FillEllipse draws a circle at every point over which the mouse cursor moves (while the mouse button is pressed).

The first parameter to method FillEllipse is a SolidBrush object, which specifies the color of the shape drawn. The SolidBrush fills an elliptical region that lies inside a bounding rectangle. The bounding rectangle is specified by the x- and y-coordinates of its upper-left corner, its height and its width. These are the final four arguments to method FillEllipse.

The x- and y-coordinates represent thelocation of the mouse event and can be taken from the mouse-event arguments (e.X ande.Y). To draw a circle, we set the height and width of the bounding rectangle so that theyare equal—in this example, both are 4 pixels.

26 27 ' set shouldPaint to True28 Private Sub FrmPainter_MouseDown(ByVal sender As Object, _29 ByVal e As System.Windows.Forms.MouseEventArgs) _30 Handles Me.MouseDown31 32 shouldPaint = True33 End Sub ' FrmPainter_MousDown34 35 ' set shouldPaint to False 36 Private Sub FrmPainter_MouseUp(ByVal sender As Object, _37 ByVal e As System.Windows.Forms.MouseEventArgs) _38 Handles Me.MouseUp39 40 shouldPaint = False41 End Sub ' FrmPainter_MouseUp42 43 End Class ' FrmPainter

Event handling for Mouse event MouseUp sets variable

shouldPaint to False

Event handling for Mouse event MouseDown sets variable

shouldPaint to True

Demo

Mouse Down Event Example

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown

Label1.Text = "x(" & e.X & "," & e.Y & ")"

' moves the label to the location that the mouse was clicked on

Label1.Location = e.Location End Sub

An example that shows how to use e to get the point that the mouse was clicked on:

Demo

An example that shows how to use e to detect which button was pressed:

• The only thing you need is to access a property of the e variable.

• Inside of the Button1_MouseDown Subroutine, type the following code:

If e.Button = Windows.Forms.MouseButtons.Right Then MsgBox("Right Button Clicked") End If• As soon as you type the letter “e”, you’ll see this pop up box:

• To detect which button was clicked, you need the first Property on the list: Button.

• Double click this property to add it to your code. • Then after you typed the equals sign, another pop up

list appears. This one:

• When you’re finished writing your code, run your program.

• Click the button with your Left mouse button and nothing will happen.

• Click it with the Right mouse button and you should see the message box display.

• Try it !

Keyboard-Event Handling

• Key Events– Generated when keys are pressed and released,

there are two types:1. KeyPress

• Can return a Char for any ASCII character pressed

2. KeyUp and KeyDown• Test for special modifier keys• Use KeyEventArgs

Keyboard-Event Handling

Keyboard-Event Handling

1 Public Class FrmKeyDemo23 ' event handler for key press4 Private Sub FrmKeyDemo_KeyPress(ByVal sender As System.Object, _5 ByVal e As System.windows.Forms.KeyPressEventArgs) _6 Handles Me.KeyPress7 8 lblCharacter.Text = "Key pressed: " & e.KeyChar9 End Sub10 11 ' display modifier keys, key code, key data and key value12 Private Sub FrmKeyDemo_KeyDown(ByVal sender As System.Object, _13 ByVal e As System.Windows.Forms.KeyEventArgs) _14 Handles Me.KeyDown15 16 lblInformation.Text = ""17 ' if key is Alt18 If e.Alt Then19 lblInformation.Text &= "Alt: Yes" & vbCrLf20 Else21 lblInformation.Text &= "Alt: No" & vbCrLf22 End If23

KeyDown event handlerdisplays information fromits KeyEventArgs object

KeyPress event handleraccesses the KeyChar property

of the KeyPressEventArgs objectand displays the Key

24 ' if key is Shift25 If e.Shift Then26 lblInformation.Text &= "Shift: Yes" & vbCrLf27 Else28 lblInformation.Text &= "Shift: No" & vbCrLf29 End If30 ' if key is Ctrl31 If e.Control Then32 lblInformation.Text &= "Ctrl: Yes" & vbCrLf & _33 Else34 lblInformation.Text &= "Ctrl: No" & vbCrLf & _35 End If3637 "KeyCode: " & e.KeyCode.ToString & vbCrLf & _38 "KeyData: " & e.KeyData.ToString & vbCrLf & _39 "KeyValue: " & e.KeyValue4041 End Sub ' FrmKeyDemo_KeyDown

The KeyCode property returnsa Keys enumeration, which must be

converted to a String via method ToString

42 ' clear labels when key is released43 Private Sub FrmKeyDemo_KeyUp(ByVal sender As System.Object, _44 ByVal e As System.windows.Forms.KeyEventArgs) _45 Handles Me.KeyUp46 47 lblInformation.Text = ""48 lblCharacter.Text = ""49 End Sub ' FrmKeyDemo_KeyUp50 End Class ' FrmKeyDemo

Both labels are clearedwhen a key is released

Demo

Example- Show MsgBox if F1 is pressed on TextBox1

• To see what properties the e variable has available to it, add the following to your

TextBox1_KeyDown code:If e.KeyCode = Keys.F1 Then TextBox1.Clear() MsgBox("Help!!!")End If

• As soon as you type the full stop after the letter “e”, you’ll see this pop up box:

•Double click a property to add it to your code. After you type an equals sign, you’ll getanother pop up box:

• Try your program out. • Click inside of the textbox and then press F1.• When the F1 key is pressed, you should see

the message box appear.

Create and handle controls in runtime

• Use the Class Name and Method Name drop-down menus to create and register event handlers.

• The information the programmer needs to register an event is the EventArgs class (to define the event handler) and the EventHandler delegate (to register the event handler).

Conclusion

• Mouse events (such as clicks and presses) use class MouseEventArgs (MouseEventHandler delegate) and EventArgs (EventHandler delegate).

• Class MouseEventArgs contains information about the x- and y-coordinates, the button used and the number of clicks.

Conclusion

• Key events are generated when keyboard’s keys are pressed and released.

• Event KeyPress can return a Char for any ASCII character pressed. One cannot determine from a KeyPress event whether special modifier keys (such as Shift, Alt and Control) were pressed.

• Events KeyUp and KeyDown test for special modifier keys (using KeyEventArgs). The delegates are KeyPressEventHandler (KeyPressEventArgs) and KeyEventHandler (KeyEventArgs).

Conclusion

• Class KeyEventArgs has properties KeyCode, KeyData and KeyValue.

• The KeyCode property returns the key pressed, but does not give any information about modifier keys.

• The KeyData property includes data about modifier keys.

• The KeyValue property returns the key code for the key pressed as an Integer.

Conclusion