Hello.
Im new to this coding stuff, so I want to know how to make controls moveable
when the program is actually running. My aim is to make a panel with a label and button
moveable. Its kinda like a widgit :)
Im using Visual Basic 2010 Express.
Thanks!
(PS: I hope it is possible)
https://www.youtube.com/channel/UCD3D2tIaTT2T6e06FZWBhsw/featured

Recommended Answers

All 4 Replies

It's certainly possible. Create a form and put a few controls of any type on it. Then add the following code.

Public Class Form1

    Dim indrag As Boolean   'true while dragging control
    Dim startX As Integer   'x powition at drag start
    Dim startY As Integer   'y position at drag start

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'assign handlers for drag and drop to all controls

        For Each Control As Control In Me.Controls
            AddHandler Control.MouseDown, AddressOf startDrag
            AddHandler Control.MouseMove, AddressOf Dragging
            AddHandler Control.MouseUp, AddressOf endDrag
        Next

    End Sub

    Private Sub startDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        'Mouse down - set drag active and save start position

        indrag = True
        startX = e.X
        startY = e.Y

    End Sub

    Private Sub Dragging(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        'move control to new position

        If indrag Then
            sender.Location = New Point(sender.Location.X + e.X - startX, sender.Location.Y + e.Y - startY)
            Me.Refresh()
        End If

    End Sub

    Private Sub endDrag(ByVal sender As System.Object, ByVal e As System.EventArgs)

        'Mouse up - set drag inactive

        indrag = False

    End Sub

End Class

This will allow you to reposition any control. If you only want to move specific controls then do AddHandler only for those controls.

If you need to save the positions of the controls from run to run then you can add a Settings variable. Name it ControlLocations and make it of type System.Collections.Specialized.StringCollection with an initial value of 0 (zero). Each entry in the collections consists of the control name, the control x position and the control y position. The fields are separated by !. Add the following to the form load event handler

For Each Control As Control In Me.Controls
    For Each item In My.Settings.ControlLocations
        If Split(item, "!")(0) = Control.Name Then
            Control.Location = New Point(Split(item, "!")(1), Split(item, "!")(2))
        End If
    Next
Next

and you'll have to change the endDrag handler to save the new positions

indrag = False
My.Settings.ControlLocations.Clear()

For Each Control As Control In Me.Controls
    My.Settings.ControlLocations.Add(Control.Name & "!" & Control.Location.X & "!" & Control.Location.Y)
Next

My.Settings.Save()

Awesome!
Ill try it out :) Thanks.

Ok! It works, so.
Thanks a LOT!
:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.