Mouse events trouble

mariano_donati

Active member
Joined
Nov 30, 2005
Messages
41
Programming Experience
Beginner
Hi, I'm making a custom trackbar. It works fine, until now at least. But i'm having some kind of trouble with the scroll button and mouse events. The thing is that i need to know mouse position, i do that handling MouseMove event of main form. But, if i move the mouse in a button or another control of the main form, then, obviously, MouseMove event of main form doesnt raise, and that blows my application off. I tried to solve it doing this:

MySubEventhandler(...) Handles MyBase.MouseMove, Control1.MouseMove, Control2.MouseMove 'And so on...


But that didn't work either, because MouseEventArgs arg, contains info about a determined control. So what i need is that even if the mouse is on a button or another control of the main form, MouseMove event keeps return me X and Y position of the mouse in the main form and not in the control that it's on.

Is that possible?
 
Yes it is possible but it will take some ingenuity on your part. First have a seperate mouse move event handler for your form... THen make a generic one and add the mouse move event from the controls to that one. For example:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
End Sub
Private Sub Cont_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
End Sub

Notice that the second one dosn't have a handles statement... Leave it that way. Now let's continue.
In the Form Load addhandlers for every controls mouse move event to that generic sub....

If the first main sub display the mouse position x and y like you have. In the second diplay it plus the senders location

here is the full code tested and good.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each cont As Control In Me.Controls
AddHandler cont.MouseMove, AddressOf Cont_MouseMove
Next
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
Label1.Text = e.X & " " & e.Y
End Sub

Private Sub Cont_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim X As Integer = sender.Location.X + e.X
Dim Y As Integer = sender.Location.Y + e.Y
Label1.Text = X & " " & Y
End Sub
 
Back
Top