Dragging - moving controls
Use this board to post your code snippets - tips and tricks
6 posts
Page 1 of 1
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
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:

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
MouseDown
YourControl.Location = New Point(YourControl.Location.X + (e.X - a), YourControl.Location.Y + (e.Y - b))
capt = 0
Code: Select all
MouseMove
a = e.X
b = e.Y
capt = 1
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
Happy dragging 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

Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
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
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
Hello,
Errm oh yes lol. Under Public capt=0 Put:

Errm oh yes lol. Under Public capt=0 Put:
Code: Select all
I forgot that bit Public a
Public b

Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
Hello mandai,
Well wouldnt make much difference but yes either way works cooll;
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.
Hi Codenstuff
Thanks for that, I have an idea for another Game and that is just what i am after
Cheers
Chris
Thanks for that, I have an idea for another Game and that is just what i am after

Cheers
Chris
6 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023