Solved

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

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

20 Experts available now in Live!

Get 1:1 Help Now