Results 1 to 20 of 20

Thread: Dragging

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    256

    Dragging

    I want to drag a button from one area of a form to another. I suppose this requires a calculation of the cursor position, and then some code for the Form object. But I cannot find out a way to do this seemingly obvious task. I know to set drag property of my button to "1", but beyond that it is a mystery to me.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    256
    C'mon crowd....no one has ever dragged an object from one part of a form to another?

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Do you mean you want to physically move the location of the button on the screen at run time?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    256
    Yes....via dragging.

    It is a program for football coaches who need to move the X's and O's around the screen.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Well ejy, this is certainly a new one for me, but try this:
    VB Code:
    1. Private oldX As Long
    2. Private oldY As Long
    3. Private IsMoving As Boolean
    4.  
    5. Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    6. oldX = X
    7. oldY = Y
    8. IsMoving = True
    9. End Sub
    10.  
    11. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    12. If IsMoving Then
    13.   Command1.Top = Command1.Top - (oldY - Y)
    14.   Command1.Left = Command1.Left - (oldX - X)
    15. End If
    16. End Sub
    17.  
    18. Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    19. IsMoving = False
    20. End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    256
    Thanks..I'll give it a shot

  7. #7
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    Yeah me too, it interest me!

    I already try your way Hack in previous app!

    But i wanted to try with the drag!

    VB Code:
    1. Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.    Command1.Drag 1
    3. End Sub
    4.  
    5.  
    6. Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    7.     Source.Top = Y - (Source.Height / 2)
    8.     Source.Left = X - (Source.Width / 2)
    9. End Sub

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Your way works well too Sebs, and it requires less code than mine.

    Less is more I always say!

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    The code I always used is similar to Hacks
    VB Code:
    1. Dim OldX As Integer
    2. Dim OldY As Integer
    3.  
    4. Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    5.     OldX = X
    6.     OldY = Y
    7. End Sub
    8.  
    9. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    10.     If Button = vbLeftButton Then
    11.         Command1.Left = Command1.Left + (X - OldX)
    12.         Command1.Top = Command1.Top + (Y - OldY)
    13.     End If
    14. End Sub
    The thing I don't like about sebs' code is that when you release the mouse the control centers itself on the mouse cursor. When I move a control, I want it to stay where it is at when I release the mouse.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Originally posted by MarkT
    The thing I don't like about sebs' code is that when you release the mouse the control centers itself on the mouse cursor. When I move a control, I want it to stay where it is at when I release the mouse.
    I wasn't going to mention that, but as long as you did...

  11. #11
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425
    Hey, I was searching for a solution to this problem. Ony I don't use a command control, but I use a shape. And to make it even more dificult, I have set the shape's index to 0.

    My app creates square shapes at runtime, how can I move those dynamically created shapes to be move by dragging????

    Can anybody help me with this, because the MouseDown etc don't work on containers, or so I think...

    Plz, help me out here...
    "Experience is something you don't get until just after you need it."

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    You are right, shapes don't have any events. I think the easiest way to deal with this one is just make your own shape OCX and give it the events you need and use some of the code above.

  13. #13
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Attached is a simple example of what I had suggested.
    Attached Files Attached Files

  14. #14
    Member
    Join Date
    Mar 2002
    Location
    Germany
    Posts
    33
    hello Sabs,

    now what if i have to move two images or buttons one after another. if we use source we can only move one of the image or button and the other one is disabled.

    if u have another way of doing it do let me know.

    Cheers !

    Dinesh Manoharan

  15. #15
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    i have a few things to do, i'll try to figure out something then!

  16. #16
    Member
    Join Date
    Mar 2002
    Location
    Germany
    Posts
    33
    ok Sebs, waiting for ur reply.

    Cheers !
    Dinesh Manoharan.

  17. #17
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425
    Thank you MarkT. It really helped me.
    Thank you very much!
    "Experience is something you don't get until just after you need it."

  18. #18
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    Originally posted by m_dinesh
    hello Sabs,

    now what if i have to move two images or buttons one after another. if we use source we can only move one of the image or button and the other one is disabled.

    if u have another way of doing it do let me know.

    Cheers !

    Dinesh Manoharan
    I don't get what you're trying to do!!

    I you want to move one and then move the other one, this
    works for me:

    VB Code:
    1. Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.    Command1.Drag 1
    3. End Sub
    4.  
    5. Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    6.    Command2.Drag 1
    7. End Sub
    8.  
    9. Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    10.     Source.Top = Y - (Source.Height / 2)
    11.     Source.Left = X - (Source.Width / 2)
    12. End Sub

  19. #19
    Member
    Join Date
    Mar 2002
    Location
    Germany
    Posts
    33
    hi,

    this worked for me
    '#####################

    Private Sub Image1_MouseMove(index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
    Image1(index).Left = X + Image1(index).Left
    Image1(index).Top = Y + Image1(index).Top
    End If
    End Sub

    Private Sub Pictures_Click()
    lclickcount = lclickcount + 1
    index = lclickcount - 1
    Label1.Caption = lclickcount

    Image1(index).Picture = Pictures.Picture
    Image1(index).Enabled = True

    imgX = Image1(index).Left
    imgY = Image1(index).Top

    End Sub

    '#####################
    in this example i have a picture of which i make several copies and then i move the copies.

    this is just a part of the code and i used the command above to move the copies

    Cheers!

    Dinesh Manoharan.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    256
    How do I leave a "trail" in the path of the object being moved?

    For example, I move an object across the screen and I want a line drawn to mark its path. Can curves be drawn, in addition to straight lines?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width