How to drag a control

Post your questions regarding programming in C# in here.
7 posts Page 1 of 1
Contributors
User avatar
Coolblade
VIP - Site Partner
VIP - Site Partner
Posts: 126
Joined: Mon Oct 24, 2011 7:04 pm

How to drag a control
Coolblade
How do I move a picture box on a double click? In vb 10
Last edited by Coolblade on Sun Nov 20, 2011 9:56 pm, edited 1 time in total.
http://www.codingpalace.tk/
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

Re: How to move a control
Bogoh67
with your mouse or to a predefined point?
User avatar
Coolblade
VIP - Site Partner
VIP - Site Partner
Posts: 126
Joined: Mon Oct 24, 2011 7:04 pm

Re: How to move a control
Coolblade
I want to know the code to move the picturebox around in the forum
http://www.codingpalace.tk/
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

Re: How to drag a control
Bogoh67
PictureBox1.Location = New Point(x, y)
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: How to drag a control
M1z23R
This will work with 'triple click'
Code: Select all
Dim dClick As Boolean
    Dim drag As Boolean
    Dim mousex As Integer
    Dim mousey As Integer

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If Not dClick = True Then Exit Sub
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - PictureBox1.Left
        mousey = Windows.Forms.Cursor.Position.Y - PictureBox1.Top
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If drag Then
            PictureBox1.Top = Windows.Forms.Cursor.Position.Y - mousey
            PictureBox1.Left = Windows.Forms.Cursor.Position.X - mousex
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        If dClick = True And drag = True Then
            drag = False
            dClick = False
        End If
    End Sub
    Private Sub PictureBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
        dClick = True
    End Sub
When you double click it will start detection of mouse down
Then, if you hold mouse down after double click it will move till you release the click. 70% is from other site, i just modified it to your needs
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: How to drag a control
Axel
Code: Select all
Public Class DragControls
    Public btf As Boolean = True
    Public button As MouseButtons = MouseButtons.Left
    Public drag As Boolean = True

    Private controlX As Control
    Private ptor As Point
    Private dragcursor As Boolean = True
    Private origCursor As Cursor
    Private state As Clicked
    Private Enum Clicked
        Up
        Down
    End Enum
    Sub SetCtrl(ByVal control As Control)
        controlX = control
        origCursor = controlX.Cursor
        AddHandler controlX.MouseDown, AddressOf MouseDoubleClick
        AddHandler controlX.MouseUp, AddressOf MouseUp
        AddHandler controlX.MouseMove, AddressOf MouseMove
    End Sub
    Private Sub MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        If drag Then
            If e.Button = button Then
                state = Clicked.Down
                ptor = e.Location
                If dragcursor Then
                    controlX.Cursor = Cursors.SizeAll
                End If
            End If
        End If
    End Sub
    Private Sub MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        state = Clicked.Up
        controlX.Cursor = origCursor
    End Sub
    Private Sub MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
        If state = Clicked.Down Then
            Dim location As Point
            location = controlX.Parent.PointToClient(controlX.MousePosition)
            location.X -= ptor.X
            location.Y -= ptor.Y
            controlX.Location = location
            If btf Then
                controlX.BringToFront()
            End If
        End If
    End Sub
End Class
put that outside the Public class form1

then somewhere in Form_Load
Code: Select all
Dim dragger as new DragControls
dragger.SetCtrl(picturebox1)
I modified this code from my controldragger class
: viewtopic.php?f=53&t=6032
http://vagex.com/?ref=25000
User avatar
Coolblade
VIP - Site Partner
VIP - Site Partner
Posts: 126
Joined: Mon Oct 24, 2011 7:04 pm

Re: How to drag a control
Coolblade
Thank you all
http://www.codingpalace.tk/
7 posts Page 1 of 1
Return to “General coding help”