4

I'm having a little problem. I set the cursor to wait status. After calling the PrintDialog the cursor returns in default status. I can't set the cursor to wait status again. The code is like this:

Cursor.Current = Cursors.WaitCursor
[...]
Dim result As DialogResult = printDialog.ShowDialog()
If result = DialogResult.Cancel Then
    Return
End If
Cursor.Current = Cursors.WaitCursor
[...]
General Grievance
  • 4,555
  • 31
  • 31
  • 45
ufo
  • 674
  • 2
  • 12
  • 35
  • 1
    If the code reaches `Return` nothing else will be executes on that method. Maybe is that your problem? – SysDragon Jan 28 '13 at 09:03
  • No, I want the waitcursor only if `result` is not cancel. The `Return` is not reached in my tests. – ufo Jan 28 '13 at 09:06

1 Answers1

2

I just did a small test with your code. When using your code my VS2012 didn't show up Cursor.Current but did not throw any exception when using it. So I changed it to

Me.Cursor = Cursors.WaitCursor

Dim result As DialogResult = printDialog.ShowDialog()
If result = DialogResult.Cancel Then
    Return
End If
' not necesary any more
'Cursor.Current = Cursors.WaitCursor

and the WaitCursor stayed after showing the printDialog.

EDIT: Found a pretty good explanation on difference between Cursor.Current and Cursor!

EDIT2: I changed my code to make use of HourGlass class from @HansPassant's example stated above. WaitCursor now stays even if you enter a textBox. Anyways - I was still able to get loss of the waitCursor when hovering over the border of eg. a textBox.

All in all IMO I think it's not very good to force a waitCursor when it is still possible to enter text aso. Perhaps you may consider disabling controls until some kind of actions has finished and afterwards change cursor back.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Hourglass.Enabled = True

    Dim result As DialogResult = PrintDialog1.ShowDialog()
    If result = Windows.Forms.DialogResult.Cancel Then
        Return
    End If

    'Cursor.Current = Cursors.WaitCursor
End Sub

Hourglass.vb - I hope I did not make any mistakes when converting it to vb.net

Public Class Hourglass
  Implements IDisposable

  Public Shared Property Enabled As Boolean
    Get
        Return Application.UseWaitCursor
    End Get
    Set(ByVal value As Boolean)
        If value = Application.UseWaitCursor Then Return
        Application.UseWaitCursor = value
        Dim f As Form = Form.ActiveForm
        If Not f Is Nothing AndAlso f.Handle <> IntPtr.Zero Then
            SendMessage(f.Handle, 32, f.Handle, 1)
        End If
    End Set
  End Property

  <System.Runtime.InteropServices.DllImport("user32.dll")>
  Private Shared Function SendMessage(hWnd As IntPtr, msg As IntPtr, wp As IntPtr, lp As IntPtr) As IntPtr
  End Function

  Public Sub Dispose() Implements IDisposable.Dispose
    Enabled = False
  End Sub
End Class
Community
  • 1
  • 1
Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
  • Thank you for the reply. Unfortunatly, the code you posted was my first choice, but in this case the cursor is not in wait status when over a text edit. Is there a solution? – ufo Jan 28 '13 at 10:32