Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Events. Events Single Event Handlers Click Event Mouse Events Key Board Events Create and handle controls in runtime Outline."— Presentation transcript:

1 Events

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

3 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.

4 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

5 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

6 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

7

8 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

9 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

10 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.

11 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

12

13 1 ' Fig. 12.33: Painter.vb 2 ' Using the mouse to draw on a form. 3 4 Public Class FrmPainter 5 6 7 Dim shouldPaint As Boolean = False 8 9 ' Visual Studio.NET IDE generated code 10 11 ' draw circle if shouldPaint is True 12 Private Sub FrmPainter_MouseMove( _ 13 ByVal sender As System.Object, _ 14 ByVal e As System.Windows.Forms.MouseEventArgs) _ 15 Handles Me.MouseMove 16 17 ' paint circle if mouse pressed 18 If shouldPaint Then 19 Dim graphic As Graphics = CreateGraphics() 20 21 graphic.FillEllipse _ 22 (New SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4) 23 End If 24 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 the location of the mouse event and can be taken from the mouse-event arguments (e.X and e.Y). To draw a circle, we set the height and width of the bounding rectangle so that they are equal—in this example, both are 4 pixels.

14 26 27 ' set shouldPaint to True 28 Private Sub FrmPainter_MouseDown(ByVal sender As Object, _ 29 ByVal e As System.Windows.Forms.MouseEventArgs) _ 30 Handles Me.MouseDown 31 32 shouldPaint = True 33 End Sub ' FrmPainter_MousDown 34 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.MouseUp 39 40 shouldPaint = False 41 End Sub ' FrmPainter_MouseUp 42 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

15 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

16 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:

17 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:

18 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 !

19 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

20 Keyboard-Event Handling

21

22 1 Public Class FrmKeyDemo 2 3 ' event handler for key press 4 Private Sub FrmKeyDemo_KeyPress(ByVal sender As System.Object, _ 5 ByVal e As System.windows.Forms.KeyPressEventArgs) _ 6 Handles Me.KeyPress 7 8 lblCharacter.Text = "Key pressed: " & e.KeyChar 9 End Sub 10 11 ' display modifier keys, key code, key data and key value 12 Private Sub FrmKeyDemo_KeyDown(ByVal sender As System.Object, _ 13 ByVal e As System.Windows.Forms.KeyEventArgs) _ 14 Handles Me.KeyDown 15 16 lblInformation.Text = "" 17 ' if key is Alt 18 If e.Alt Then 19 lblInformation.Text &= "Alt: Yes" & vbCrLf 20 Else 21 lblInformation.Text &= "Alt: No" & vbCrLf 22 End If 23 KeyDown event handler displays information from its KeyEventArgs object KeyPress event handler accesses the KeyChar property of the KeyPressEventArgs object and displays the Key

23 24 ' if key is Shift 25 If e.Shift Then 26 lblInformation.Text &= "Shift: Yes" & vbCrLf 27 Else 28 lblInformation.Text &= "Shift: No" & vbCrLf 29 End If 30 ' if key is Ctrl 31 If e.Control Then 32 lblInformation.Text &= "Ctrl: Yes" & vbCrLf & _ 33 Else 34 lblInformation.Text &= "Ctrl: No" & vbCrLf & _ 35 End If 36 37 "KeyCode: " & e.KeyCode.ToString & vbCrLf & _ 38 "KeyData: " & e.KeyData.ToString & vbCrLf & _ 39 "KeyValue: " & e.KeyValue 40 41 End Sub ' FrmKeyDemo_KeyDown The KeyCode property returns a Keys enumeration, which must be converted to a String via method ToString 42 ' clear labels when key is released 43 Private Sub FrmKeyDemo_KeyUp(ByVal sender As System.Object, _ 44 ByVal e As System.windows.Forms.KeyEventArgs) _ 45 Handles Me.KeyUp 46 47 lblInformation.Text = "" 48 lblCharacter.Text = "" 49 End Sub ' FrmKeyDemo_KeyUp 50 End Class ' FrmKeyDemo Both labels are cleared when a key is released

24 Demo

25 Example- Show MsgBox if F1 is pressed on TextBox1

26 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

27 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 get another pop up box:

28 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.

29 Create and handle controls in runtime

30

31

32

33 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

34 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

35 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

36 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


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

Similar presentations


Ads by Google