Download presentation
Presentation is loading. Please wait.
Published byElwin Harris Modified over 9 years ago
1
Events with Data Arguments Data Values Travel with e
2
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
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
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
5 KeyEventArgs Data The KeyDown event is raised by any key. The e.KeyCode field identifies the key. KeyDownArgs
6
More Drawing Part 12 dbg
7
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
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
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
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
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
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
13
Paced Iterations Sleeping a Thread
14
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
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
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
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.
18
Animated Drawing Draw and Clear
19
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
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
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
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
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
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.
25
Paced Iterations Timers
26
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
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
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
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
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
31 Timer Object Because the Timer object is totally created in code, the Elapsed event handler function must be created manually.
32
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
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
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
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
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
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
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
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.