How to drag a control
Post your questions regarding programming in C# in here.
7 posts
Page 1 of 1
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.
I want to know the code to move the picturebox around in the forum
This will work with 'triple click'
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
Code: Select all
When you double click it will start detection of mouse downDim 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
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
Code: Select all
put that outside the Public class form1Public 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
then somewhere in Form_Load
Code: Select all
I modified this code from my controldragger class Dim dragger as new DragControls
dragger.SetCtrl(picturebox1)
: viewtopic.php?f=53&t=6032
7 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023