改变光标 VB.NET

5

当 ToolStripButton 被点击时,我无法更改光标。

我有两个按钮叫做“Rechercher”。

编辑:只有按钮起作用。似乎是 ToolStripButton 影响了我的光标...感谢帮助!

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

这不是唯一的区别。AccessBtn在运行过程之前设置光标。你的程序中是否有任何DoEvents调用? - LarsTech
也许你应该在你的子程序中尝试使用MsgBox("hello")来测试事件处理程序(测试当你点击时是否会发生某些事情)。 - FeRtoll
2个回答

5
也许你可以尝试一些更简单的东西,比如:
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

问题在于你没有任何暂停代码执行的地方,因此它执行得太快了。

我不理解使用sleep的意义。我要浪费一秒钟等待吗? - Adam Paquette
可能是你的代码在try和finally之间执行得太快了,所以你看不到变化。为了测试它是否有效,你可以使用sleep进行测试 :) 希望你明白我的意思。 - FeRtoll
当光标被设置后,我等待2秒钟让我的代码完成。当事件来自我的工具栏按钮时,我无法更改光标,但在其他情况下它可以工作。 - Adam Paquette
无论您从哪里单击,是按钮还是工具栏,您总是调用Rechercher()或其他子程序。在光标更改或任何视觉更改之后放置Application.DoEvents(),它将起作用。我设置Threading.Thread.Sleep(1000)只是为了测试,因为代码可能需要1秒钟才能完成。您可以尝试将其设置为5000,然后您将看到光标。 - FeRtoll

3
这是我使它起作用的唯一方法。
<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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接