Changing the cursor to an hourglass

Posted in: General vb.NET
By dePoPo
Mar 11, 2009 - 12:09:11 PM

Depending on the exact location of the code and what you want to archieve, use one of the following;

  • Method #1 changes the state of the cursor for the selected form only, moving the cursor off the form will have it change into the state set by the form you are then over.

  • Method #2 changes the state of the cursor for all of your application


       ' --- Method #1, change the cursor for the owner object (eg a form) This is the same as
       '     Setting the cursor property at design time (see image)
      
 
        ' change cursor to hourglass
        Me.Cursor() = Cursors.WaitCursor()
        ' change back to normal
        Me.Cursor() = Cursors.Default()
 


        ' --- This may generaly be better
 
        ' change cursor to hourglass
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
 
        ' change back to normal
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default



cursorpropertie.jpg



Many times usage of Application.Doevents will also mess with the cursor state, so if you want to make sure you can maintain a waitcursor continiously in some operation, move any application.doevenst's  outside the worker loop like:


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' --- Have other stuff updated
        Application.DoEvents()

        ' --- Set wait cursor (HourGlass)
        Cursor.Current = Cursors.WaitCursor

        ' --- Do important stuff here

        ' --- Set the cursor back to normal
        Cursor.Current = Cursors.Default


    End Sub


Visitor Comments