Hi all
I am changing color of label on mouse move event
But my question is how to get back the previous color when mouse in not on the label
Miss I want when mouse is on the label then its color is red otherwise previous color
See my code
VB Code:
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
i dont want any event on form load
when mouse is on then label then label color is rad
when mouse is other place of the form miss not on the label then it's color is previous color
The usual solution to do this kind of stuff is to use the SetCapture API function. Unfortunatly that only works with controls that have a hWnd, which a Label doesn't. There is however another little trick you can use that involves setting the DragIcon property to a mouse pointer of your choice. This is an important step otherwise this whole thing will look very funny. I've attached a hand icon you can use if you like.
Now for the trick, in the MouseMove event you start a Drag & Drop operation, this is because the Label will then start to fire the DragOver event, and this event will let you know when the mouse leaves the control, in which case you stop the dragging. You will actually never move the Label, you just start this operation to fire the DragOver event. The downside to this is that the MouseMove or Click events will not fire anymore. So if you use the Click event move that code into the DragDrop event instead, since that will fire when you press the mouse button.
VB Code:
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.BackColor = vbRed
Label1.Drag vbBeginDrag
End Sub
Private Sub Label1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
If State = vbLeave Then
Label1.BackColor = vbButtonFace
Label1.Drag vbEndDrag
End If
End Sub
Private Sub Label1_DragDrop(Source As Control, X As Single, Y As Single)
'Use this event instead of the Click event
Debug.Print "Label1_Click"
End Sub
Just remember to set the DragIcon property (during design-time) before using this code.
hi joacim ur coding is good but label is going with the mouse
That's because you haven't set the DragIcon property, which I mentioned twice that you'll have to set, otherwise you get that ugly outline of the label when you move the mouse over it.
Hey!!! Shakti u are not understanding what anderson is saying. U first download the
hand.zip file and then apply the icon in ur dragicon property. The window is not opening.
It is a common dialog.
Dont rely only on your luck. Work hard until You get success.
Yes, but I mentioned SetCapture in my first post. I also mentioned that a Label doesn't have a hWnd so you can't use it. So I posted another work-around, and you asked what how to use it with a Label. The answer is already available in this thread.
As i was going to say, JA was answering the same question. He'd already mentioned the SetCapture stuff. He suggested a work-around avoiding SetCapture, I suggested a work-around using SetCapture.
Originally Posted by vivek_master146
I also want to ask that how much experienced u r in VB programming.
umm, learnt a bit out of necessity when in a data entry job a couple of years back then took a break. decided to make an app for my g/f that would help her manage her teaching (she's a teacher) last year and have just learnt stuff as i go along. In the grand scheme of things probably v. little experience.