MOUSE CURSORS

A form’s Cursor property determines the kind of mouse cursor the form displays. The Form class inherits the Cursor property from the Control class, so other controls have a Cursor property, too. If you want to give a particular control a special cursor, you can set its Cursor property. For example, if you use a Label control as a hyperlink, you could make it display a pointing hand similar to those displayed by web browsers to let the user know that the control is a hyperlink.

The Cursors class provides several standard cursors as shared values. For example, the following statement sets a form’s cursor to the system default cursor (normally an arrow pointing up and to the left):

Me.Cursor = Cursors.Default

Figure 9-2 shows example program ShowCursors, which is available for download on the book’s website, displaying the names and images of the standard cursors defined by the Cursors class in Windows 8. In other versions of Windows, some of the cursors may appear differently.

FIGURE 9-2: The Cursors class defines standard cursors.

image

Unless a control explicitly sets its own cursor, it inherits the cursor of its container. If the control is placed directly on the form, it displays whatever cursor the form is currently displaying. That means you can set the cursor for a form and all of its controls in a single step by setting the form’s cursor.

Similarly, if a control is contained within a GroupBox, Panel, or other container control, it inherits the container’s cursor. You can set the cursor for all the controls within a container by setting the cursor for the container.

One common use for cursors is to give the user a hint when the application is busy. The program sets its cursor to Cursors.WaitCursor when it begins a long task and then sets it back to Cursors.Default when it finishes. The UseWaitCursor example program, which is available for download on the book’s website, uses the following code to display a wait cursor when you click its button:

Me.Cursor = Cursors.WaitCursor
' Perform the long task.
...
Me.Cursor = Cursors.Default

If the program displays more than one form, it must set the cursors for each form individually. It can set the cursors manually, or it can loop through the My.Application.OpenForms collection. The UseMultipleWaitCursors example program, which is available for download on the book’s website, uses the following SetAllCursors subroutine to display a wait cursor on each of its forms when you click its button:

Private Sub SetAllCursors(the_cursor As Cursor)
    For Each frm As Form In My.Application.OpenForms
        frm.Cursor = the_cursor
    Next frm
End Sub

The following code shows how the program uses the SetAllCursors subroutine while performing a long task:

SetAllCursors(Cursors.WaitCursor)
' Perform the long task.
...
SetAllCursors(Cursors.Default)

To use a custom cursor, create a new Cursor object using a file or resource containing cursor or icon data. Then assign the new object to the form’s Cursor property. The SmileCursor example program, which is available for download on the book’s website, uses the following code to display a custom cursor:

Me.Cursor = New Cursor(My.Resources.SmileIcon.Handle)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.151.220