drag and drop question

Posted on 2006-04-06
Last Modified: 2010-04-07
I am using vb 6 and I want to be able to drag and drop controls on a form at runtime(for this example a textbox or a label) I have an image of a ruler on the top and side of the form and when I drag the textbox, etc... to a spot on the ruler it always snaps back a few and never lines up correctly. I tried a couple of examples I found on this site and it didn't work. Can someone post a simple example of how to do this? I want the control to stay at the point it is dropped not move.

Thank you,

Question by:heydude
    1 Comment
    LVL 85

    Accepted Solution

    Assuming your Label (Label1) is a Control Array:

    Option Explicit

    Private startX As Single
    Private startY As Single

    Private Sub Label1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            startX = X
            startY = Y
        End If
    End Sub

    Private Sub Label1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            Label1(Index).Move Label1(Index).Left + (X - startX), Label1(Index).Top + (Y - startY)
        End If
    End Sub

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now