Jump to content
  • Advertisement
Sign in to follow this  
jagguy2

drag a control

This topic is 3668 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!