Creating Event Handlers in the Windows Form Designer

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 8.  Adding Events

Creating Event Handlers in the Windows Form Designer

Visual Basic 6 required that you switch to the code view to add event handlers for controls. This modality still exists in Visual Basic .NET.

Note

Beta versions of Visual Basic .NET also occasionally support managing events from the Properties view. Unfortunately, the Event button (see Figure 8.2) showed up in the Properties window in beta versions of Visual Basic .NET, although it was not supposed to. The Event button supports listing events in the Properties window and generating event handlers from the Properties window. Currently this behavior is only intentionally supported in C#.

Figure 8.2. Generate event handlers from the Event list in the Properties window.

graphics/08fig02.jpg


Use the left two buttons in the Properties window to order the events by category or alphabetically , respectively, depending on your tastes. Find the property that you want to implement a handler for and double-click in the Properties window adjacent to the event name . The Form Designer will generate the event-handling procedure, including the appropriate WithEvents statement (discussed later in this chapter).

Let's combine the MouseMove and Paint events with GDI+ to display the location of the mouse directly on the form. Complete the following steps to create the example:

  1. Create a new Windows Application template and name it FormEvents.

  2. Click on the blank form, Form1 by default.

  3. Press F4 to display the Properties window.

  4. Click on the Events button and the alphabetically ordered listing button.

  5. Find the MouseMove event and double-click next to the event name to generate the empty MouseMove event. (The complete code is shown in Listing 8.7.)

  6. All we want the form to do is repaint so that the mouse position text is updated on the form. Call the Form method Invalidate as demonstrated in listing 8.7.

  7. Repeat step 5 to generate a Paint event handler for the form.

  8. The argument e is a PaintEventArgs object. PaintEventArgs contains an instance of the Graphics object that we can use instead of calling CreateGraphics, as demonstrated in Listing 8.4. Draw the mouse position using the Graphics. DrawString method as demonstrated in Listing 8.7.

  9. Compile and run the example.

The code displays the formatted X, Y coordinate of the mouse relative to the screen {0, 0} position, which is the upper-left corner of your monitor. Listing 8.7 is followed by a brief overview of some of the code in the listing you might not be familiar with.

Listing 8.7 Using event handlers and GDI+ to track the mouse's movement.
  1:  Private Sub Form1_MouseMove(ByVal sender As Object, _  2:  ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove  3:   4:  Invalidate()  5:  End Sub  6:   7:  Private Sub Form1_Paint(ByVal sender As Object, _  8:  ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint  9:   10:  e.Graphics.DrawString(MousePosition().ToString(), Me.Font, _  11:  Brushes.Black, 10, 10)  12:  End Sub 

Lines 1 through 5 demonstrate the MouseMove event handler. Invalidate is a method of the Form; line 4 could also be written as Me.Invalidate(). Invalidate causes the form to be repainted. Line 10 uses the graphics object that is a member of PaintEventArgsin Listing 8.4 we used a factory method, CreateGraphics, to get an instance of a graphics object. The Graphics.DrawString method is called. Forms also have a MousePosition method, introduced in the Control class. DrawString writes to the device context of the owning object. In the listing, the device context is the form's DC. The Me in Me.Font was used for clarity; we are passing the form's Font property, a global Brushes object, and constant X, Y offsets (both 10) to write the position of the mouse.

Device context is the conceptual representation of the drawing surface in Windows. The Win32 API refers to this as DC, and a handle to a device context is usually hDC. For example, in VB6 we might declare the API procedure TextOut, which takes a device context argument to determine the virtual surface to draw on, to perform a similar operation. (See Chapter 17, "Programming with GDI+," for more on the Graphics object and device contexts.)

Tip

Invalidate is introduced in the Control class. All controls have an Invalidate method, which sends a paint message to a control.


Recall that ToString prints a representation of the instance. MousePosition is a Point object. ToString for Point objects represents the Point object as a formatted X and Y offset, {X=10, Y=10} . (Interestingly enough, the various implementations of ToString serve as a good demonstration of polymorphic behavior. Recall, for instance, that DateTime.ToString prints a date and time value.)

All event handlers can be created from the Properties view. You can also manually write the code to define event handlers or create event handlers in the Code designer in a manner identical to the way event handlers were created in VB6.


Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net