Sign in to follow this  
Dan Verssen

Visual Basic 2005 - Moving a Control

Recommended Posts

After much looking and fiddling, I have a program that I can drag controls around the screen with the mouse... >> Public Class Form1 Dim StartX, StartY As Integer Dim Dragging As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown Dragging = True StartX = e.X StartY = e.Y End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If Dragging = True Then PictureBox1.BringToFront() PictureBox1.Left = (PictureBox1.Left + e.X) - StartX PictureBox1.Top = (PictureBox1.Top + e.Y) - StartY End If End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp Dragging = False End Sub Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click End Sub Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click End Sub End Class << My next question is... Is there a way I can apply the MouseDown, MouseMove, and MouseUp subs to all the controls on the screen? That way I don't have to copy those sections for each control.

Share this post


Link to post
Share on other sites
I found this bit of code, but I'm not sure how to tie it in to the above code...

>>

'first assign the same event handler to every control on the form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctl As Control In Me.Controls
AddHandler ctl.MouseDown, AddressOf MyMouseDown
AddHandler ctl.MouseUp, AddressOf MyMouseUp
Next
AddHandler Me.MouseDown, AddressOf MyMouseDown
AddHandler Me.MouseUp, AddressOf MyMouseUp
End Sub

<<

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this