Movable controls but read ...

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
3 posts Page 1 of 1
Contributors
User avatar
Agust1337
Coding God
Coding God
Posts: 2456
Joined: Fri Feb 19, 2010 8:18 pm

Movable controls but read ...
Agust1337
How do i make movable controls like
dim btn as new button
make the btn movable?
Top-notch casual Dating
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: Movable controls but read ...
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;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Agust1337
Coding God
Coding God
Posts: 2456
Joined: Fri Feb 19, 2010 8:18 pm

Re: Movable controls but read ...
Agust1337
ok ty :D
Top-notch casual Dating
3 posts Page 1 of 1
Return to “Tutorial Requests”