|
Hi
I have a strange problem which i am not sure if it is an issue in VB
Public Class Form1
Private moving As Boolean = False
Private Sub Label2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label2.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
moving = True
End If
End Sub
Private Sub Label2_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label2.MouseUp
moving = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label3.Text = moving
End Sub
Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
Dim eLocation As New System.Drawing.Point(e.X, e.Y)
If moving = True Then
Label1.Text = "X." & e.X & vbCrLf & "Y." & e.Y
Label2.Location = eLocation
End If
End Sub
End Class
There is a label on the form and when you click on it is should be dragable on the form. The issue is that the flag is not been set on the mouse down event but I am not sure why or if it is the mouse move event where I test for the flag is always false. If I take the test for the flag out of the mouse move the label follows the mouse with the left mouse button down so all events seem to be working. If I declare the flag as true at start up the label also follows the mouse until the left button is pressed and released resetting the flag. I have also tried removing the flag reset in mouse up event, this actually starts to work when the left button is released.
Any help would be appreciated
Col
VB Express 2010
|
|
|
|
|
Have a read of this
Drag label anywhere[^]
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
Hi
Yes i have Simon and i have seen a lot of examples however they all seem to stop at VB 2008, I have tried the example but the problem i have is still there. It seems that on the mouse down button it fires the mousedown event, while holding the the button and now move the mouse the mousemove event is not triggered untill the mouse button has been released. Is there any way to tell the event to end and contine looking for other events?? is this a limitation of 2010 Express version??
Col
|
|
|
|
|
You need to implement a mouse move event on the control not the form
i.e.
Private Sub Control_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label2.MouseMove
If Moving Then
Dim ctrl As Control = CType(sender, Control)
' Move the control according to mouse movement
ctrl.Left = (ctrl.Left + e.X) - cursorX
ctrl.Top = (ctrl.Top + e.Y) - CursorY
' Ensure moved control stays on top of anything it is dragged on to
ctrl.BringToFront()
End If
End Sub
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
Thanks Simon, that was the problem the control had to have the event
Col
|
|
|
|
|
Your Form1_MouseMove method handles the form's MouseMove event. Shouldn't it be attached to Label2 MouseMove?
Alan.
[EDIT, just like Simon says]
|
|
|
|