Hi guys,
Just wondering if there is a onMouseover event for msaccess/vb? i want to display a comment (label) that is set to invisible initially, but when the user puts their mouse over a button (not clicking on it) then it displays the comment or label on the form. I have already tried the on got focus, and on mouse move but to no avail.
Cheers
GK
Cheers that works.
I just realised you can use the Control Tip Text on the other tab in properties for a button. this will display a comment on mouse over for that button.
Champinco
What is this label for is it something that will change. Or is it a set thing to say what the thing the user is hovering over does. If it is the second then you could use a tool tip. This would show a line of text when the user hovers over the button or whatever you want. It like when you hover over a button in word on the toolbar and it says what that is used for.
Hope this is relevant
Sam
Sorry just realised you have written a reply while I was writing this
If you want a real solution (SetCapture and ReleaseCapture have their problems, it is available for one program and one control only at a time), take a look at SelfSub. It shows you the one and only way to do it right Of course, subclassing might feel like too much and too complex as itself, but it is actually only real Windows programming, it is a look into how all Windows programs really work.
Hi guys,
Im getting problems with the code below...the error method or data member not found. in particular to hWnd variable. why is this so becuase this is declared in the 1st function SetCapture as (ByVal hWnd As Long) As Long.
Code:
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long
Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (X < 0 Or X > Command2.Width) Or (Y < 0 Or Y > Command2.Height) Then
Call ReleaseCapture
Label9.Visible = False
ElseIf Command2.hWnd <> GetCapture Then
Call SetCapture(Command2.hWnd)
Label9.Visible = True
End If
End Sub
Hi guys,
Im getting problems with the code below...the error method or data member not found. in particular to hWnd variable. why is this so becuase this is declared in the 1st function SetCapture as (ByVal hWnd As Long) As Long.
you mentioned access at the start - is this VBA?
if so then you won't be able to use SetCapture because the hWnds for UserForm controls are not exposed.
zynder,
yes this works...but only that it shows the label when the mouse is over the button, but does not get rid of the label once the mouse is off the button again.
bushmobile,
yes this is VBA. so this wont work? what i want is a label displayed onmouseover for a button, and then label not displayed once the mouse is not on it again. i have multiple buttons on a form and wish to switch on and off different labels for the corresponding buttons. I dont want to use Control Tip text because the user has to scroll over the button and wait several seconds before a message (label) is displayed. I wish to have an instantaneous message (label) on mouse over for a button then dissapear after they are no "mousing over" longer on that button.
do you mean both subs or the first/second?
also do i call it Command2_UserForm_MouseMove or just UserForm_MouseMove?
i.e:
Code:
Private Sub Command2_UserForm_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label9.Visible = True
End Sub
Private Sub Command2_UserForm_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label9.Visible = False
End Sub
this does not work: (yes it does show the label but once i move off the button the label is still displayed.
Code:
Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label9.Visible = True
End Sub
Private Sub UserForm_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label9.Visible = False
End Sub
You'd have to add the Label9.Visible = False code to all other controls, or atleast all the controls that are near the button, ie. if you have the button in a frame, you have to add the code to the frame's MouseMove event procedure.
Yeah, it isn't a very elegant solution, but since subclassing goes way ahead of your skill level, there really isn't many options...
ok...so going back to bushmobiles comment: if so then you won't be able to use SetCapture because the hWnds for UserForm controls are not exposed.
then how would i work around this for my other code/post.
Hi guys,
Im getting problems with the code below...the error method or data member not found. in particular to hWnd variable. why is this so becuase this is declared in the 1st function SetCapture as (ByVal hWnd As Long) As Long.
Code:
Code:
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long
Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (X < 0 Or X > Command2.Width) Or (Y < 0 Or Y > Command2.Height) Then
Call ReleaseCapture
Label9.Visible = False
ElseIf Command2.hWnd <> GetCapture Then
Call SetCapture(Command2.hWnd)
Label9.Visible = True
End If
End Sub
as attached.
there are different buttons, and i wish to simply have a mouse over event displaying a label once the user mouses over the button, and then also dissapears once they are off the button. similar to a control tip text event.
I wish to have a different label/message for each button.
cheers
GK
you need to place the name of the label you want to be associated with the button in the button's Tag property. for example, I placed Label1 into the Tag property of CommandButton1.
the code
VB Code:
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" ( _
lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" ( _
ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private bRunning As Boolean
Private lhWnd As Long
Private Sub UserForm_Initialize()
lhWnd = FindWindow(vbNullString, Me.Caption)
End Sub
Private Sub UserForm_Activate()
If Not bRunning Then
bRunning = True
TimerLoop
End If
End Sub
Private Sub TimerLoop()
Dim ptCursor As POINTAPI, ptClient As POINTAPI, ptBlank As POINTAPI
Dim lX As Long, lY As Long
Do While bRunning
DoEvents
GetCursorPos ptCursor
ClientToScreen lhWnd, ptClient
lX = (ptCursor.X - ptClient.X) * 0.75
lY = (ptCursor.Y - ptClient.Y) * 0.75
If lX > -1 And lY > -1 Then GenericMouseOver lX, lY
ptClient = ptBlank
Loop
Unload Me
End Sub
Private Sub GenericMouseOver(ByVal lX As Long, lY As Long)
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is CommandButton Then
If lX > ctl.Left And lX < (ctl.Left + ctl.Width) And _
lY > ctl.Top And lY < (ctl.Top + ctl.Height) Then
If Not Me.Controls(ctl.Tag).Visible Then Me.Controls(ctl.Tag).Visible = True
Else
If Me.Controls(ctl.Tag).Visible Then Me.Controls(ctl.Tag).Visible = False
End If
End If
Next ctl
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If bRunning Then
Cancel = True
bRunning = False
End If
End Sub
the code works - it's just a question of whether you'll be able to integrate it correctly.
Not sure if anyone is still checking this, but here is what I use for my form mousemove VBA. It's not nearly as complex as some of the other postings, but it works for me.
When the form opens I make the label invisible. If the user moves the mouse over the command button, then the button text is bolded, the text color changes, and the label is set to visible. If the user moves the mouse over the detail section of the form, then the label is set back to invisible and the button text is set back to normal.
-----------------------------------------------------------------------
Option Compare Database
Option Explicit
'Created By Jeff McCoy
Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error GoTo Err_Command0
With Me.Controls("Command0")
.ForeColor = 16711680
.FontWeight = 700
End With
Me.txt1.Visible = True
Exit_Command0:
Exit Sub
Err_Command0:
MsgBox Err.Description
Resume Exit_Command0
End Sub
Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error GoTo Err_Detail
With Me.Controls("Command0")
.ForeColor = 0
.FontWeight = 400
End With
Me.txt1.Visible = False
Exit_Detail:
Exit Sub
Err_Detail:
MsgBox Err.Description
Resume Exit_Detail
End Sub
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open
Me.txt1.Visible = False
Exit_Form_Open:
Exit Sub
Err_Form_Open:
MsgBox Err.Description
Resume Exit_Form_Open
End Sub
Not sure if anyone is still checking this, but here is what I use for my form mousemove VBA. It's not nearly as complex as some of the other postings, but it works for me.
When the form opens I make the label invisible. If the user moves the mouse over the command button, then the button text is bolded, the text color changes, and the label is set to visible. If the user moves the mouse over the detail section of the form, then the label is set back to invisible and the button text is set back to normal.
This method does not work reliability. If you hover over the command button and then move the mouse swiftly off the detail section of the form to another area of the screen the command button will remain with the hover attributes. This is because the move move events are not fired for the detail section because the mouse was not in the detail section long enough. This solution works as long as you only slowly move the mouse around. The proper solution is to use Windows events to detect the hover. Unfortunately this is also more complex. See http://www.codeproject.com/Articles/...-to-VB-Control for an implementation.
The proper solution is to use Windows events to detect the hover. Unfortunately this is also more complex.
Using a VB.TimerCtl which destroys itself (when nothing is hovered),
one can avoid TrackMouseEvent and the SubClassing-approach.
Code for a Form (create two PictureBoxes on it):
Code:
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (pPoint As Any) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal X&, ByVal Y&) As Long
Private ctlHov As Object, WithEvents tmrHov As VB.Timer
Private Sub MouseOver(Sender As Object)
If TypeOf Sender Is PictureBox Then Sender.BackColor = vbGreen
Debug.Print Sender.Name, "MouseOver"
End Sub
Private Sub MouseOut(Sender As Object)
If TypeOf Sender Is PictureBox Then Sender.BackColor = Me.BackColor
Debug.Print Sender.Name, "MouseOut"
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
SetHoverWatch Picture1
End Sub
Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
SetHoverWatch Picture2
End Sub
Private Sub SetHoverWatch(ControlUnderMouse As Object)
If tmrHov Is Nothing Then Set tmrHov = Controls.Add("VB.Timer", "tmrHov")
tmrHov.Interval = 50
If Not ctlHov Is ControlUnderMouse Then
If Not ctlHov Is Nothing Then MouseOut ctlHov
MouseOver ControlUnderMouse
End If
Set ctlHov = ControlUnderMouse
End Sub
Private Sub tmrHov_Timer()
Dim Pt(0 To 1) As Long: GetCursorPos Pt(0)
On Error GoTo 1
If ctlHov.hWnd = WindowFromPoint(Pt(0), Pt(1)) Then Exit Sub
1 If Not ctlHov Is Nothing Then MouseOut ctlHov: Set ctlHov = Nothing
Controls.Remove "tmrHov": Set tmrHov = Nothing
End Sub