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

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Objective 8.03 Apply Animation and Graphic Methods in a Windows Form (4%)
Advertisements

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Mouse event handling Mouse events( delegates EventHandler, event arguments EventArgs) MouseEnter: raised if the mouse cursor enters the area of the control.
Tutorial 15 Working with the Event Model. XP Objectives Compare the IE and W3C event models Study how events propagate under both event models Write a.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Programming Based on Events
Mouse draw Using the mouse and a palette to make a picture.
Graphics and Multimedia. Outline Introduction Graphics Contexts and Graphics Objects Color Control.
 2002 Prentice Hall. All rights reserved. 1 Chapter 12 – Graphical User Interface Concepts: Part 1 Outline 12.1Introduction 12.2 Windows Forms 12.3 Event-Handling.
Mouse event handling Mouse events( delegates EventHandler, event arguments EventArgs) MouseEnter: raised if the mouse cursor enters the area of the control.
MsgBox Function Displays one of Visual Basic’s predefined dialog boxes, which contains a message, one or more command buttons, and an icon After displaying.
110-C1 Chapter 2 of the text: _ text boxes, group boxes, check boxes, radio buttons, picture boxes _ defining access keys tab sequence setting the focus.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Chapter 2 –Visual Basic, Controls, and Events
CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.
Apply Sub Procedures/Methods and User Defined Functions
Graphics and Multimedia. Outline Introduction to Multimedia Loading, Displaying and Scaling Images Windows Media Player Adding a Flash Movie Microsoft.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
CSCI 3327 Visual Basic Chapter 10: Windows Forms GUI: A Deeper Look UTPA – Fall 2011.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
CSC 298 Windows Forms.
BIM211 – Visual Programming Objects, Collections, and Events 1.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Lecture Set 13 Drawing Mouse and Keyboard Events Part B – Mouse and Keyboard Events.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 - Graphical User Interface Concepts: Part.
Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Events with Data Arguments Data Values Travel with e.
COM148X1 Interactive Programming Lecture 7. Topics Today HCI Event Handling.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
BIM211 – Visual Programming Interacting with Users Graphics 1.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
16.9 Introduction to Multimedia Visual Basic offers many convenient ways to include images and animations in programs Computing field decades ago mainly.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Events with Data Arguments Data Values Travel with e.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
CISC 110 Day 6 Introduction to Events. Outline Event-Driven Programming Event Classes Hierarchy –Event Class –Mouse Events –Keyboard Events Registering.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
CIS 338: Events Dr. Ralph D. Westfall April, 2011.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Dr. Abraham Professor UTPA Graphical User Interface.
 2007 Pearson Education, Inc. All rights reserved Graphical User Interface Concepts: Part 1.
Visual Basic Fundamental Concepts
Programming Right from the Start with Visual Basic .NET 1/e
A variable is a name for a value stored in memory.
Keyboards, Pages & Transforms
Computer Programming I
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Visual Basic Properties, Methods and Events
Visual Basic..
Fundamentals of Programming in VB.Net
Chapter 13: Handling Events
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Presentation transcript:

Events

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

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

1 ' Fig : Painter.vb 2 ' Using the mouse to draw on a form. 3 4 Public Class FrmPainter Dim shouldPaint As Boolean = False 8 9 ' Visual Studio.NET IDE generated code ' 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 ' paint circle if mouse pressed 18 If shouldPaint Then 19 Dim graphic As Graphics = CreateGraphics() graphic.FillEllipse _ 22 (New SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4) 23 End If 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.

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 shouldPaint = True 33 End Sub ' FrmPainter_MousDown ' 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 shouldPaint = False 41 End Sub ' FrmPainter_MouseUp 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

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 ' 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 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

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 "KeyCode: " & e.KeyCode.ToString & vbCrLf & _ 38 "KeyData: " & e.KeyData.ToString & vbCrLf & _ 39 "KeyValue: " & e.KeyValue 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 lblInformation.Text = "" 48 lblCharacter.Text = "" 49 End Sub ' FrmKeyDemo_KeyUp 50 End Class ' FrmKeyDemo Both labels are cleared when 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 get another 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