Page 1 of 1

Movable controls but read ...

Posted: Sat Mar 27, 2010 12:36 am
by Agust1337
How do i make movable controls like
dim btn as new button
make the btn movable?

Re: Movable controls but read ...

Posted: Sat Mar 27, 2010 10:32 am
by CodenStuff
Hello,

If your adding the controls dynamically then you will need to add event handlers for them manually.

To add a moveable button you can try this. First under "Public Class Form" add this:
Code: Select all
Dim a
    Dim b
    Dim capt = 0
Then add this code wherever you want to create your button, like at form load or a button click or something:
Code: Select all
Dim btn As New Button
        btn.Size = New Size(100, 100) 'CHANGE TO THE HEIGHT AND WIDTH OF YOUR BUTTON
        btn.Location = New Point(100, 100) 'CHANGE TO THE LOCATION FOR IT TO BE CREATED
        AddHandler btn.MouseDown, AddressOf btn_Down
        AddHandler btn.MouseUp, AddressOf btn_Up
        AddHandler btn.MouseMove, AddressOf btn_Move
        Me.Controls.Add(btn)
Then add these event handlers within your main code:
Code: Select all
 Private Sub btn_Down(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        a = e.X
        b = e.Y
        capt = 1
    End Sub
    Private Sub btn_Up(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        CType(sender, Button).Location = New Point(CType(sender, Button).Location.X + (e.X - a), CType(sender, Button).Location.Y + (e.Y - b))
        capt = 0
    End Sub
    Private Sub btn_Move(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If capt = 1 Then
            CType(sender, Button).Location = New Point(CType(sender, Button).Location.X + (e.X - a), CType(sender, Button).Location.Y + (e.Y - b))
        End If
    End Sub
That should do it cooll;

Re: Movable controls but read ...

Posted: Sat Mar 27, 2010 5:53 pm
by Agust1337
ok ty :D