Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-31
2
Medium Priority
?
308 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:Murray Brown
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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:Murray Brown
ID: 39218834
Thanks very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

824 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