Events with Data Arguments Data Values Travel with e.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Create a Simple Game in Scratch
Create a Simple Game in Scratch
Microsoft® Small Basic
IS660Z Programming Games Using Visual Basic Overview of Cannonball.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Chapter 9 Color, Sound and Graphics
COMPUTER PROGRAMMING I Objective 8.03 Apply Animation and Graphic Methods in a Windows Form (4%)
Chapter 3 Creating a Business Letter with a Letterhead and Table
Microsoft® Small Basic
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Lecture Roger Sutton CO331 Visual Programming 16: Timer Control 1.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Events. Events Single Event Handlers Click Event Mouse Events Key Board Events Create and handle controls in runtime Outline.
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
Controls General Discussion. VB Controls Visual Basic Controls A control is the generic name for any object placed on a form Controls may be images,
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Creating a MagicInfo Pro Screen Template
1 Windows Printing. 2 Objectives You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
1 Windows Graphics. 2 Objectives You will be able to Use the Windows GDI+ to draw arbitrary figures and text on a Windows form. Add a handler for the.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
CSC 298 Windows Forms.
BIM211 – Visual Programming Objects, Collections, and Events 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 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Visual Basic .NET BASICS
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.
BIM211 – Visual Programming Interacting with Users Graphics 1.
Digital Electronics and Computer Interfacing Tim Mewes 4. LabVIEW - Advanced.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
CIS 3.5 Lecture 2.2 More programming with "Processing"
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.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
© 2011 Delmar, Cengage Learning Chapter 10 Using ActionScript to Enhance User Experience.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Events with Data Arguments Data Values Travel with e.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
More Control Properties Part 10 dbg. Form Properties that Affect TitleBar ControlBox Property (T/F) Do you display the icon, associated menu to the left,
Slide 1 VB Graphics Controls & Timer Control. Slide 2 Default Controls.
Images Part 11 dbg. 2 Images The form and several controls can display a number of different types of image files in the BackgroundImage Property. These.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
Compound Data Types Part13dbg. 2 Point A Point is a simple built-in struct that stores a pair of screen coordinates. Instantiate a Point: Point aPoint.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Scratch for Interactivity
Computer Programming I
Graphics and Multimedia
Control Structures
Variables and Arithmetic Operations
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
Items, Group Boxes, Check Boxes & Radio Buttons
ICT Gaming Lesson 3.
Presentation transcript:

Events with Data Arguments Data Values Travel with e

2 EventArgs Every event handler function has an e argument in its signature. Some events use the System.EventArgs type for e. This type is empty — it has no data fields. The click event uses an e of this type and thus carries no data.  UseOfSender

3 MouseEventArgs Data The MouseDown event delivers data in e. e is of type MouseEventArgs. e has data fields for the x and y coordinates of the mouse and the identity of the mouse button pressed. X and Y are expressed in terms of pixels, measured from the upper left corner of the Client Area of the form.  MouseDownArgs

4 KeyPressEventArgs Data Pressing an ANSI key when a form has focus raises the KeyPress event. The e.KeyChar field identifies the key that is pressed. Note that the KeyPreview property of the form must be true so that the handler will detect the event before it is intercepted by any other objects on the form.  KeyPressArgs

5 KeyEventArgs Data The KeyDown event is raised by any key. The e.KeyCode field identifies the key.  KeyDownArgs

More Drawing Part 12 dbg

7 What Have We Learned So Far about Drawing? Create a drawing surface by declaring a Graphics Object and running CreateGraphics() method. Use Pens for outline shapes, lines, and text. Use Brushes for filled shapes. Use Graphics Object Methods to draw or color shapes and lines, and to draw text.

8 Problems Associated with Drawing Suppose you want to draw something, say an hp logo on form, starting 1/3 way down from upper left corner Where you put code for determining start coordinates (based on form size) makes a difference. If you put code in Load event, drawing will stay in original location, even when form is resized, because form size values are not updated. To update form size values, put start coordinate calculations in Resize handler, which “fires” each time form is resized.

9 Problems Associated with Drawing How do you prevent the old drawing from being visible when you draw again after form is resized? You must erase the previous drawing before drawing the new one. To erase a drawing use the Clear() method with the BackColor of drawing surface as an argument.  hp logo oops!

10 Problems Associated with Drawing What happens when we make the form smaller? Drawing may disappear entirely. This is because redrawing of controls (and graphics on form) is accomplished by the Paint event. The Paint event is “fired” only when necessary to repair “damage” to the form, such as when a form is resized larger. To force redrawing when you make the form smaller too, you must call the Paint event handler from the Resize event handler.  hp logo problem

11 Paint Event Drawing is generally done in the Paint event. Paint event is “fired” each time the form is displayed (initially), resized larger, moved, minimized, restored, or uncovered. The Paint event can be called from other event handlers. When your drawing is based on the size of the form/control, it is a good idea to call the Paint event from the Resize event handler (because the Resize handler updates form ClientSize values).

12 Calling the Paint Event Handler from Another Function The Paint event has different EventArgs than other events. Before calling the Paint event from another handler, you must provide some information that Paint will need, in the PaintEventArgs.  hp logo fixed

Paced Iterations Sleeping a Thread

14 Threads A thread is essentially a separate unit of execution. A single application can have several threads. The processor can split time among the threads. This allows other tasks to begin before a particular lengthy task is completed.

15 Sleeping a Thread A “default” Windows application has a single thread. This can be inferred by the behavior of the application when a modal object, like a MessageBox, is displayed. One might suspect that the execution of an application could be paced by somehow causing its thread to be blocked periodically.

16 Sleeping a Thread The Thread class exists in the System.Threading namespace. The Sleep() method of the Thread object allows us to block execution of a thread for a specific duration. Sleep() takes a single argument, an integer value representing milliseconds.  SleepFor  SleepWhile

17 Sleeping a Thread Be aware that this technique stops the entire application if it has a single thread. Later we will learn how to create multiple threads so that we could simultaneously, but independently, pace several actions in a single application. Notice that we can make the application aware of the System.Threading namespace with a using statement.

Animated Drawing Draw and Clear

19 Animation with Draw and Clear Create a moving box by drawing a rectangle, clearing the Graphics object, incrementing one of the starting coordinate values, and then repeating the process. This process could be iterated with a for loop to move the box a preset distance. Pace the animation by sleeping the thread.  DrawClear

20 Animation with Redraw Redraw animation allows simultaneous animations. Draw a shape with a colored Pen. After a short pause, redraw the shape with a Pen set to the BackColor. Increment one of the starting coordinates and repeat the process.  Redraw

21 Animation with Indefinite Iteration Use redraw animation to simulate a bouncing ball. Control the animation with a while loop. Recall that focus must be assigned to the stop button as soon as the animation is started.  WhileRedraw

22 Balloon Inflation Animation This animation requires that both starting coordinates and the width and height all be changed for each animation step. Because the radius of a circle increases at half the rate of the diameter (width & height of the rectangle the balloon is drawn in), we can increment the height and width by 2 pixels while decrementing the starting coordinate positions by 1 pixel.  ConcentricDraw

23 While with User Intervention Two complications arise when we wish to demonstrate stopping a While loop with user intervention. We must first inform the processor that we will be looking for a second action during the time that the iteration is running. This is necessary because the processor often schedules its activities without regard to the desired sequence of action of the application.

24 Application.DoEvents The DoEvents method of the Application object forces the processor to accept instructions in strict order of their receipt. A call to this method within iterated code is often necessary to cause sequential execution.

Paced Iterations Timers

26 Timers A Timer is an object that raises a single event at a regular interval. There are 2 different ways to use timers: 1.Use a Timer component from the Toolbox 2.In code, instantiate a Timer object from the System.Timers.Timer class.

27 Timer Component The Timer component is represented by an icon in the Toolbox. Dragging a Timer from the Toolbox and dropping it on a form creates an instance of the Timer in your application. The Timer is represented in your application by an icon in the component tray of the form designer.

28 Timer Component When the component tray icon for the Timer is selected, you can adjust its properties in the property window. The Timer has Start() and Stop() methods. The Timer has an Interval property to control the duration (in milliseconds) between each firing of its event. The Timer component raises the Tick event.  TimerComponent

29 Iterating with a Timer Any code that could be iterated by enclosing in the braces of a While loop can also be iterated by including it in the Tick event handler for a Timer. The iteration is automatically paced at the speed dictated by the Interval property setting of the Timer.

30 Timer Object A Timer object may also be instantiated in code from the System.Timers.Timer class. Any property settings for this Timer will have to be made in code. Although it works exactly the same as the Timer component, the Timer object raises the Elapsed event rather than the Tick event.  TimerObject

31 Timer Object Because the Timer object is totally created in code, the Elapsed event handler function must be created manually.

32 Timer Object The event handler object must be added to the code manually as well. The ElapsedEventHandler can be added in the Form_Load event handler.

33 Suicide Timers A suicide timer is able to turn itself off after completing a certain iteration. A Timer object has an extra property, AutoReset, that may be set to False. This will stop the Timer after a single interval has elapsed. This is handy for timing a single occurrence.  SplashPanel

34 Suicide Timers The iterated code within a Timer may have a logical check for some endpoint for the iteration. The Timer can run its own Stop() method to turn itself off when the logical check is satisfied.  SuicideStop

35 Format Specifiers Specifiers are case insensitive. Adjust the number of decimal places by appending an integer to the specifier where appropriate. The result of the format must be assigned to a string variable or a string type control property.  FormatSpecifiers

36 Multiple Variables in One Format More than one specifier can be used. Enclose each specifier with a position integer in braces in the correct position of the string. Add a variable to match each specifier.  MultipleSpecifiers

37 Custom Formats You can create a custom format for a numeric value using the ToString() method. Each numeric type has a ToString() method. With no arguments, ToString() simply creates a string value that looks just like the numeric value.

38 Custom Formats A custom format string may also be used as an argument. The 0 and # characters are used to display digits of the value. 0 is a digit placeholder that will always be displayed or replaced by a digit from the value. # is a digit placeholder that will only cause display of existing digits.

39 Custom Formats Truncation can occur if there are not enough placeholders—either 0 or # to accommodate the digits of the numeric value. Other characters in the string (commas, decimals, parentheses, hyphens, etc., are displayed as written in the format string.  CustomFormats