gpt4 book ai didi

excel - VBA-MouseMove 打开和关闭另一个用户窗体

转载 作者:行者123 更新时间:2023-12-04 20:31:14 25 4
gpt4 key购买 nike

我有一个带有几个标签控件的用户窗体,它们都属于一个类,在鼠标悬停时,将显示另一个包含有关该标签的一些信息的用户窗体。现在我希望在鼠标离开控件后关闭该表单。现在我正在使用 application.ontime 并在 2 秒后关闭第二个表单,这使得当鼠标仍在标签上时表单会闪烁。我想知道是否有更好的方法?到目前为止,这是我的代码。

我在类模块上的代码

Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
Dim m
On Error Resume Next
If Button = XlMouseButton.xlPrimaryButton And LabelBase.Edit.Caption = "Done" Then
Label1.Left = Label1.Left + X - x_offset
Label1.Top = Label1.Top + Y - y_offset
ElseIf LabelBase.Edit.Caption = "Edit" Then
With CurrentJob
.Caption = "Current Job of " & Label1.Caption
.LBcurr.list = openJobs
.LLast = LastJob
.LClsd = WorksheetFunction.CountIfs(oprecord.Range("e:e"), Label1.Caption, oprecord.Range("f:f"), Date, oprecord.Range("s:s"), "CLOSED")
.LAc = Fix(Right(Label1.Tag, Len(Label1.Tag) - 1) / 24) + 70006
m = WorksheetFunction.VLookup(Label1.Caption, rooster.Range("b:e"), 4, 0)
.LSkill = Right(m, Len(m) - InStr(1, m, " "))
.StartUpPosition = 0
.Top = X + 10
.Left = Y + 10
.Show
End With
With Label1
If X < .Left Or X > (.Left + .Width) Or Y > (.Top + .Height) Or Y < .Top Then closeee
End With
End If
End Sub

我在第二个用户窗体上的代码
Private Sub UserForm_Activate()
Application.OnTime Now + TimeValue("00:00:03"), "closeee"
End Sub

Private Sub UserForm_Terminate()
On Error Resume Next
With Me
clearallcontrols
End With
Application.OnTime Now + TimeValue("00:00:03"), "closeee", , False

End Sub

这是加载信息表单时 MAin 用户表单的图片。

Information_form_Partial.jpg

问候,

最佳答案

您不需要计时...如果您想使用鼠标移动,关闭信息显示表单的代码(我想它的名称是 CurrentJob )应该由 UserForm_MouseMove 触发主表单上的事件,当离开标签时,鼠标接下来将位于表单本身上(除非您将标签彼此相邻放置而没有任何空格 - 这将使下一条评论按原样显示)。

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CurrentJob.Hide
End Sub

我还建议将信息显示代码打包在自己的私有(private)子中,以保持各种标签的代码干净。

示例:我有一个带有 Label1、Label2、Label3、Textbox1 和以下代码的表单:
Private Sub ShowInfo(InfoText As String)
' code to query info and show in seperate window
' make sure window doesn't get focus
' I prefer to use non editable text boxes in my main window
Me.TextBox1 = InfoText
End Sub

Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
ShowInfo "Mouse is over Label1"
End Sub

Private Sub Label2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
ShowInfo "Mouse is over Label2"
End Sub

Private Sub Label3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
ShowInfo "Mouse is over Label3"
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
' this is the exit code
' as here we left all labels
ShowInfo "Mouse is not on a label"
End Sub

关于excel - VBA-MouseMove 打开和关闭另一个用户窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50338188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com