1 / 40

Events with Data Arguments

Events with Data Arguments. Data Values Travel with e. 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.

duer
Télécharger la présentation

Events with Data Arguments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 uses data stored in KeyPressEventArgs to identify 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

  6. More Drawing Part 12dbg

  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 directly on the form, say an hp logo, 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. Put start coordinate calculations in Paint event 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. Paint Event Tips • Drawing is generally done in the Paint event. • Windows “fires” the Paint event each time the form is displayed (initially), resized, moved, minimized, restored, or uncovered. • If you are drawing directly on the form and you do encounter issues related to resizing the form and the drawing not being “refreshed”, add one of the following statements to your ReSize event handler. this.Invalidate(); -or- this.Refresh(); • These statements mark the form “in need of repair” and the Paint event will be automatically called.

  11. Animation

  12. Techniques for Animation We will learn 3 ways to perform animation: • Sleeping a thread within a loop. • Adding a Timer component from the ToolBox and setting its Interval property and placing animation code in the Tick event. • Instantiating a Timer object from System.Timers.Timer class via code and placing animation code in the Elapsed event. This requires manual coding of event handler skeleton and “event wiring”.

  13. Animation Technique 1 Sleeping a Thread inside a Loop

  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.

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

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

  20. Redraw Animation • 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

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

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

  23. 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. • This may not be necessary when code is running on dual processor machines.

  24. Animation Technique 2 Using Timer Component

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

  26. Timer Component • The Timercomponent 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.

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

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

  29. Animation Technique 3 Adding a Timer Object via Code

  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. Formatting Output with Format Specifiers Similar to DecimalFormat and printf

  36. Formatting using 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

  37. 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. • This is a somewhat like printf in Java.  MultipleSpecifiers

  38. Custom Formats using ToString • 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. • Give a pattern, similar to DecimalFormat, in parentheses. double battingAverage; lblAverage.Text = battingAverage.ToString(“#.000”);

  39. Custom Formats using String.Format • 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.

  40. 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. • This is somewhat like DecimalFormat in Java.  CustomFormats

More Related