Dragging - moving controls

Use this board to post your code snippets - tips and tricks
6 posts Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Dragging - moving controls
CodenStuff
Hello,

This little snip will show you how to drag and move a control around your form using the mouse.

Here is the code:

Make public variables called:
Public capt = 0
Public a
Public b


Place the following codes in your chosen controls mouse events:

MouseUp
Code: Select all
YourControl.Location = New Point(YourControl.Location.X + (e.X - a), YourControl.Location.Y + (e.Y - b))
capt = 0
MouseDown
Code: Select all
 a = e.X
b = e.Y
capt = 1
MouseMove
Code: Select all
If capt = 1 Then
YourControl.Location = New Point(YourControl.Location.X + (e.X - a), YourControl.Location.Y + (e.Y - b))
End If

Replace the word 'Yourcontrol' with the name of your control. So for example if I wanted to drag and move and picturebox control the code would look like this:
Code: Select all
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        PictureBox1.Location = New Point(PictureBox1.Location.X + (e.X - a), PictureBox1.Location.Y + (e.Y - b))
        capt = 0
    End Sub
    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        a = e.X
        b = e.Y
        capt = 1
    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If capt = 1 Then
            PictureBox1.Location = New Point(PictureBox1.Location.X + (e.X - a), PictureBox1.Location.Y + (e.Y - b))
        End If
    End Sub
Happy dragging :D
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

Hi Codenstuff
ok i followed the Tutorial Above, but i get "a is not declared" and the "b" is the same

am I missing something :lol:

Chris
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Dragging - moving controls
CodenStuff
Hello,

Errm oh yes lol. Under Public capt=0 Put:
Code: Select all
Public a
Public b
I forgot that bit :D
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Dragging - moving controls
mandai
Wouldn't it be better if capt were a Boolean?
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Dragging - moving controls
CodenStuff
Hello mandai,

Well wouldnt make much difference but yes either way works cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

Hi Codenstuff
Thanks for that, I have an idea for another Game and that is just what i am after :)

Cheers

Chris
Image
6 posts Page 1 of 1
Return to “Quick Snips”