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 10-5 shows the names and images of the standard cursors defined by the Cursors class in Windows Vista. In previous versions of Windows, the AppStarting and WaitCursor values display hourglasses instead of animated circles.

image from book
Figure 10-5: The Cursors class defines standard cursors.

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 following code shows an example:

  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 SetAllCursors subroutine shown in the following code makes setting the cursor for all forms a bit easier:

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

The following code 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 following code sets a form’s cursor to the program resource named SmileIcon.ico:

  Me.Cursor = New Cursor(My.Resources.SmileIcon.Handle) 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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