简体   繁体   中英

Change cursor VB.NET

I can't change the cursor when it's a ToolStripButton.click event.

I got 2 buttons that call "Rechercher".

EDITED : Only the Button is working. It seems that the ToolStripButton cancel my cursor... Thx for the help

Public Class FenetrePrincipale

    Private WithEvents _btnRechercher As New Windows.Forms.ToolStripButton("Rechercher")
    Private WithEvents btnRechercherAccesBtn As New Button

    Private Sub Rechercher(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _btnRechercher.Click, btnRechercherAccesBtn.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            'WAITING FOR THE CODE TO FINISH (2 sec)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub
End Class

Maybe you should try something sampler like:

Private Sub MainFrame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Search()
End Sub

Private Sub Search()
Try
  Me.Cursor = Cursors.WaitCursor
  UseWaitCursor = True
  Application.DoEvents()
  Threading.Thread.Sleep(1000) 'WAITING FOR THE CODE TO FINISH
Finally
   UseWaitCursor = False
   Me.Cursor = Cursors.Default
   Application.DoEvents()
End Try
End Sub

The problem is you don't have any pause where code should execute so it is doing to fast.

This is the only way I got this to work.

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

Public Class FenetrePrincipale

    Private WithEvents _btnRechercher As New Windows.Forms.ToolStripButton("Rechercher")
    Private WithEvents btnRechercherAccesBtn As New Button

    Private Sub Rechercher(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechercherAccesBtn.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            'code...
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub RechercherToolStripButton(ByVal sender As Object, ByVal e As System.EventArgs) Handles _btnRechercher.Click
        Me.UseWaitCursor = True
        SendMessage(Me.Handle, &H20, Me.Handle, New IntPtr(1))

        Rechercher(Nothing, Nothing)

        Me.UseWaitCursor = False
    End Sub
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM