Solved

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

Posted on 2013-05-31
2
293 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now