locked
vb.net 2008 : drag controls in form how?

    Question

  • hello can somebody tell me how i can drag
    a button in the form to another place in the form

     

    thanks

     

    Thursday, June 05, 2008 8:15 PM

Answers

  • Move it as you would any control:

    Code Snippet

      Private MouseIsDownOnButton1 As Boolean

      Private MouseLocationInMouseDownEvent As Point

      Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown

        MouseLocationInMouseDownEvent = e.Location

        MouseIsDownOnButton1 = True

      End Sub

      Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove

        If MouseIsDownOnButton1 Then

          Button1.Left += e.X - MouseLocationInMouseDownEvent.X

          Button1.Top += e.Y - MouseLocationInMouseDownEvent.Y

        End If

      End Sub

      Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp

        MouseIsDownOnButton1 = False

      End Sub

     

     

     

    Friday, June 06, 2008 5:01 PM

All replies

  • Clarify.  Run or design time.  Although I don't understand under what circumstances you would move a button at run time.

    Thursday, June 05, 2008 8:43 PM
  • yes
    i wanna make a program to create dialogs
    yes i wanna move a button in run time how ?
    thanks
    Friday, June 06, 2008 4:49 PM
  • Move it as you would any control:

    Code Snippet

      Private MouseIsDownOnButton1 As Boolean

      Private MouseLocationInMouseDownEvent As Point

      Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown

        MouseLocationInMouseDownEvent = e.Location

        MouseIsDownOnButton1 = True

      End Sub

      Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove

        If MouseIsDownOnButton1 Then

          Button1.Left += e.X - MouseLocationInMouseDownEvent.X

          Button1.Top += e.Y - MouseLocationInMouseDownEvent.Y

        End If

      End Sub

      Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp

        MouseIsDownOnButton1 = False

      End Sub

     

     

     

    Friday, June 06, 2008 5:01 PM
  • thanks works great! Smile
    Friday, June 06, 2008 5:05 PM
  • Jordy26 as the original poster it is up to you to mark a post that is the correct answer to your question so forum browsers can see that your question was answered as they may have a simular question to ask and it would be easier for them to find .

    Friday, June 06, 2008 10:41 PM