I am writing a control in vb.net that will allow a user to resize something. So far, resizing works, but I can’t get moving to work properly, instead of moving where the mouse moves, it seems to flicker between two positions, but I can’t figure out where these positions are coming from. It follows the mouse sort of. I know the explanation isn’t very good so here is some code so you can see for your self: pastebin.
Apologies for this bad quality (please point out the bad stuff so I can correct), but this is really giving me a nightmare and I couldn’t figure out a clean way to write this.
If anyone has any answers as to why it wont move to the correct position, I would be very grateful!
Edit: To clear the code up a little, the moving takes place where the selected handle is “m”. Which is this part:
If SelectedHandle.Contains("m") Then
If e.Button = MouseButtons.None Then
SelectedHandle = ""
Else
Me.Location = e.Location
End If
End If
Changing it to this works, but it jumps to the middle of the control whereas I would like it to move relative to where the user clicked the mouse on the control
If SelectedHandle.Contains("m") Then
If e.Button = MouseButtons.None Then
SelectedHandle = ""
Else
Dim newloc = Me.Location
newloc.Offset(e.X, e.Y)
newloc.Offset(-0.5 * Width, -0.5 * Height)
Me.Location = newloc
End If
End If
Looks like y_difference and x_difference are calculated using the X and Y coordinates which can be relative to the context where the move (or click) happens. Check the article here.
Always using e.Location should give the correct numbers.