• Advertisement
Sign in to follow this  

drag a control

This topic is 3457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do i click on a control and drag it to another location on screen during run-time in vb.net winforms? I am making a map editor and want to drag images from 1 panel to another. Could you show me a method where you use mouse down,move and mouse up? on google i am finding a new way each time it seems and i am confused

Share this post


Link to post
Share on other sites
Advertisement
this will move a button around a panel but I cant place it on the blue panel?

Public Class Form1
Public Const WM_NCLBUTTONDOWN = &HA1
Public Const HTCAPTION = 2
Dim dragging As Boolean
Dim mouseX, mouseY As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Panel2.HorizontalScroll.Visible = True
Panel2.HorizontalScroll.Enabled = True
Panel2.HorizontalScroll.Minimum = 0
Panel2.HorizontalScroll.Maximum = 100
Panel2.BackColor = Color.Aqua
Panel1.HorizontalScroll.Visible = True
Panel1.HorizontalScroll.Enabled = True
Panel1.HorizontalScroll.Minimum = 0
Panel1.HorizontalScroll.Maximum = 100

Panel1.BackColor = Color.Chocolate


End Sub



Private Sub btnMove_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseDown
If e.Button = MouseButtons.Left Then
Dragging = True
mousex = -e.X
mousey = -e.Y
Dim clipleft As Integer = Me.PointToClient(MousePosition).X - btnMove.Location.X
Dim cliptop As Integer = Me.PointToClient(MousePosition).Y - btnMove.Location.Y
Dim clipwidth As Integer = Me.ClientSize.Width - (btnMove.Width - clipleft)
Dim clipheight As Integer = Me.ClientSize.Height - (btnMove.Height - cliptop)
Cursor.Clip = Me.RectangleToScreen(New Rectangle(clipleft, cliptop, clipwidth, clipheight))
btnMove.Invalidate()
End If
End Sub

Private Sub btnMove_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseMove
If Dragging Then
'move control to new position
Dim MPosition As New Point()
MPosition = Me.PointToClient(MousePosition)
MPosition.Offset(mousex, mousey)
'ensure control cannot leave container
btnMove.Location = MPosition
End If
End Sub

Private Sub btnMove_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseUp
If Dragging Then
'end the dragging
Dragging = False
Cursor.Clip = Nothing
btnMove.Invalidate()
End If
End Sub


End Class

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement