[Help] Movable Buttons[Solved]

Posts 1–11 of 11 · Page 1 of 1
[Help] Movable Buttons[Solved]
Okay, so I want to let a user be able to move buttons in a form whenever he clicks it and holds the mouse.
I did some research and expiremented but I didn't found anything usefull.
Can anyone please help me? =3
Ok heres some code off the top of my head.
1 First make a new timer
2 then disable the timer
3 Double click the timer
4 Type this code:
Code:
button1.location = new point(cursor.position.x, cursor.position.y)
5 Now click the button and go to your events tab and find mousedown
6 in the mousedown code type this:
Code:
timer1.enabled = true
7 Now find the mouseup code and type this:
Code:
Timer1.enabled = false
Its not the best code but it works. if you need questions ask!
Quote Originally Posted by consca View Post
Ok heres some code off the top of my head.
1 First make a new timer
2 then disable the timer
3 Double click the timer
4 Type this code:
Code:
button1.location = new point(cursor.position.x, cursor.position.y)
5 Now click the button and go to your events tab and find mousedown
6 in the mousedown code type this:
Code:
timer1.enabled = true
7 Now find the mouseup code and type this:
Code:
Timer1.enabled = false
Its not the best code but it works. if you need questions ask!
That won't work as the "Cursor.Position" is relative to your whole screen, while the button's location is only relative to the form. 1 Sec
Yeah, as i said.. at the top of my head. rymes

Button1.Location = New Point(Cursor.Position.X - Me.Location.X, Cursor.Position.Y - Me.Location.Y)

Do that its a fix


EDIITT!!!!!!

Real fix. works perfect!

Button1.Location = New Point(Cursor.Position.X - Me.Location.X - Button1.Size.Width / 2, Cursor.Position.Y - Me.Location.Y - Button1.Height * 1.5)'Adjust the 1.5 based on the size of the button
ok, here is an EVEN EPIC EASYER CODE

by ImMortAL-
Code:
Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        Xpos = Cursor.Position.X - Button1.Location.X
        Ypos = Cursor.Position.Y - Button1.Location.Y
    End Sub

    Private Sub Button16_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Pos = MousePosition
            Pos.X = Pos.X - Xpos
            Pos.Y = Pos.Y - Ypos
            Button1.Location = Pos

        End If
to use this code read this

http://www.mpgh.net/forum/33-visual-...rum-r-c-p.html
I find this is a lot smoother:

Variables
[php]
#Region "ButtonVars"
Dim ix, iy As Integer
Dim pX, pY As Integer
Dim isDragging As Boolean
#End Region
[/php]

Moving:
[php]
Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
isDragging = True
ix = Cursor.Position.X - Me.Location.X - Button1.Location.X
iy = Cursor.Position.Y - Me.Location.Y - Button1.Location.Y
End Sub

Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
If isDragging Then
Dim pp As New Point(Cursor.Position.X - Me.Location.X - ix, Cursor.Position.Y - Me.Location.Y - iy)
Button1.Location = New Point(pp)
End If
End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
isDragging = False
End Sub
[/php]
Thanks for the replies guys
I'll try some of them later on today ^^
Love MPGH :3
Quote Originally Posted by omghacker View Post
Thanks for the replies guys
I'll try some of them later on today ^^
Love MPGH :3
Really, well there is a button for it.
I am glad this is solved, and will be marked as such, however, Doesn't everyone know that MSDN is loaded with basic codes (like this, as it was the intentions of the designers) ,. In either case, /marked solved.
Quote Originally Posted by NextGen1 View Post
I am glad this is solved, and will be marked as such, however, Doesn't everyone know that MSDN is loaded with basic codes (like this, as it was the intentions of the designers) ,. In either case, /marked solved.
I think MSDN scares people (like me). I don't like their social forum thingo either, it has a really messed up layout. It's pretty much a last resort .
Meh, Memorize what you can, common sense and intellesence ;p
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us