Solved

VB.net Allowing a userto drag a control to a different part of a form

Posted on 2013-05-31
2
299 Views
Last Modified: 2013-06-04
Hi

I have a VB.net app where I would like a user to be able to re-position
any control on a Windows form. Is it possible to allow this and then get
information about the new position?
0
Comment
Question by:murbro
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39210417
Yes it's possible. You simply need to handle the MouseDown, MouseUp, and MouseMove events. You can use MouseUp to get the information about the new position.

e.g.

Public Class Form1
    Private isControlDown As Boolean = False
    Private allowMove As Boolean = False

    Private Sub TextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDown
        Me.allowMove = Me.isControlDown
    End Sub

    Private Sub TextBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseMove
        If Me.allowMove Then
            Me.TextBox1.Location = Me.PointToClient(Me.TextBox1.PointToScreen(e.Location))
        End If
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        Me.isControlDown = e.Control
    End Sub

    Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        Me.isControlDown = e.Control
    End Sub

    Private Sub TextBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseUp
        Me.allowMove = False
    End Sub
End Class

Open in new window


I added a test for the Control key being down before the control is permitted to move. You don't have to do this, and you can change it to any key you like.
0
 

Author Closing Comment

by:murbro
ID: 39218834
Thanks very much
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question