Results 1 to 37 of 37

Thread: [RESOLVED] Drag and drop at run time ..

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Resolved [RESOLVED] Drag and drop at run time ..

    well i have a lil API .. which is based on command buttons and picturboxes right now .... i mean .. i click buttons and it places ( copy pastes ) associated picture box to given coordinates .. but requirement of my deisgn is very vast . i mean i can not place a command button for every scenario ( in what order and design .. would those picture boxes be placed ) ... isnt this possible.. that at run time .. i drag and drop my picture box ( and VB alligns it to nearest grid .. or u can say coordinates ) .. once again .. i need a copy paste of my picture boxes .. also ..at run time user can be able to draw a line (which means for the interconnection between those pictureboxes ) .... infact my design is kinda electronics related .. so u can assume that its kinda connections ( which i m refferin as picture boxes ) .. and lines will work as connecting wires .... i hope i do make sense ...aint i

  2. #2
    Addicted Member Beengie's Avatar
    Join Date
    Nov 2003
    Location
    Central Valley, CA
    Posts
    243

    Re: Drag and drop at run time ..

    Your post is confusing.
    Please describe what you would like to do clearly and you will get help.
    BeengieHappy.Vaue = (SharksScore > OpponentsScore)

    Go Sharks!

  3. #3
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Drag and drop at run time ..

    You can use the MouseDown/MouseUp events to drag any control within a form.
    Also, you can search the forum for 'snapping' objects to edges of other objects.
    And you can use Line to draw lines based on the mouse coordinates (also use MouseMove and MouseDown events.

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    so here i m tryin this thing .... well .. now i m lookin for code to give behind ... jus as at run time .. i drag and drop my picture box , a ( i have three picture boxes ... a ,b and c ) ... as i drop it on form it should check this
    that which standard coordinate is the nearest one ..

    x ranges from 0 to 7
    y ranges from 0 to 11
    (1575+x*945,105+y*525) "these are the standard coordinates "

    and vb should see that droping coordinates are closer to which one of these and should move the instance of picture box to nearest standard coordinates
    lemme know .. if i m makin sense ...

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    seems pretty difficult task .. or i aint makin any sense ... caz it is virtually impossible that u guys dont know any things .. u r the GURUS ... k i have come up with an idea . y shouldnt apply distance formula to every drop zone to calculate the nearest standard point .... is there any method in vb ... which return the coordinates of drop zone ( point where i have droppped my picture box after dragging ) .. and also ..coordinates should be with respect to my form .. not with respect to my screen ... i hope this will work .. waiting ...

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    I'm sorry, but the way you have written your posts is giving me a headache. I can't understand what you want. Do you just want to be able to drag and drop a picturebox with the mouse?

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    i m sorry abt that .. will try to be more clear ... offcourse penagate . i want a drag drop from a mouse .. and see now .. as i m dragging any picturebox and drop it somewhere on the form ( assume this as dropping point ) ... VB should compare this droppoing point (coordinates ) with the set of coordinates i have given below

    x ranges from 0 to 7
    y ranges from 0 to 11
    (1575+x*945,105+y*525)

    and should move the picture box to nearest point ( from the 96 points i have given above ) ...
    u can consider these 96 points as my grid .. as vb alligns everything to nearest grid .. i want allignment to these points ....

  8. #8
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Drag and drop at run time ..

    So basically you are trying to make a function that aligns it automatically to the nearest points like VB IDE does?

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Oh I see now. Thanks for the clarifcation. I have actually posted an example of a drag-drop with grid alignment on the forums before. I will search for it.

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    yeah yeah yeah .... ... thts what i want right now .... later i ll be lookin for what u can call as ... "marked" .. i mean vb should mark those points as marked . where i have dropped a picturebox and should eliminate them from next comparison ( see i cant have two pictureboxes at one point ).. and also "undo" at run time ... removing specific picturebox from its point .. if i don want it there ....

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Bah, I couldn't find my example and couldn't be bothered looking too much for it.

    Basically what you need to do is integer divide your coordinates by a certain figure and then multiply them again by that same figure in order to get the "grid" effect.

    Here is how to drag+drop a picture box and have it snap to a grid. Make sure both your PictureBox and your Form have .ScaleMode set to vbPixels.
    VB Code:
    1. Private Sub Picture1_MouseMove( _
    2.     Button As Integer, _
    3.     Shift As Integer, _
    4.     X As Single, _
    5.     Y As Single _
    6. )
    7. Static ssngMouseX   As Single
    8. Static ssngMouseY   As Single
    9.  
    10.     If (Button = vbLeftButton) Then
    11.         X = ((X - ssngMouseX) \ 25) * 25
    12.         Y = ((Y - ssngMouseY) \ 25) * 25
    13.  
    14.         Picture1.Move (Picture1.Left + X), _
    15.                       (Picture1.Top + Y)
    16.  
    17.       Else
    18.         ssngMouseX = X
    19.         ssngMouseY = Y
    20.  
    21.     End If
    22. End Sub

    As for using predefined points, that would be extremely slow, because you need to loop through an array of stored points and compare against each point, every time you move the mouse.

  12. #12

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    well penagate this code is workin very fine ... but i want an instance of picture box to be dragged and dropped ... i have created a control array of picture boxes but it isnt loading them .. anyway .. along with that by this new grid scheme in how many columns and rows are you dividing your form ... should i redesign my form with ur given grids ... caz u see i have to place the pictureboxes in those points .... slowness is not the issue ... am i makin sense ?

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    I.. err... how many columns/rows you get depends on the width/height of the form... If you want specific numbers of columns/rows then you need to work out the width/height of each column, and use those numbers in place of where I have used 25.

    Quote Originally Posted by Saint333
    slowness is not the issue
    On the contrary, it will be if you use predefined points. Trust me, it is not pleasant to have slow code in a _MouseMove event.

  14. #14

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    ok boss .. i ll do as u say .. forget those predefined points .. now as i had 12 rows and 8 columns in my own grid (number of points as i have given earlier in my post .. ) .... so i think .. i should divide x by 8 and y by 12 .... but this will divide the whole of form in rows and columns ... i want a lil vacant space at right where i could place my command buttons and other controls ... also .. how can i fix my form widht and height ( i don want it to vary at run time and ruin all my design

  15. #15
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Set your form's .BorderStyle to Fixed Single (if you want an icon) or Fixed Dialog (if you don't).

  16. #16

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    k .. i have fixed my form's length and width .. its (630 X 399) ... and i want to have some vacant space at right ( or where ever possible left , top .. anywhere ) ... and i want portion of my form to be availble as work space .. portion ( from point(105,7) to point(546,392)) .. i mean only this portion should be divided into rows and columns and mouse movement should work only in this portion of my form ... am i makin sense ?

  17. #17
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Drag and drop at run time ..

    Sounds like you need a picturebox as workspace and not the complete form?

  18. #18
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Yes. Make a picturebox, call it picWorkspace or something, and use that as your workspace. Put all your drag+drop pictureboxes inside that.

  19. #19
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Great minds think alike...

  20. #20

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    i agree ... placed a big picture box (named it as "work") there which would work as my workspace .. now how would i clamp this pictue box there permanently .. i mean while working or testing .. it moves by clicking .. . .and also when i run and drag and drop my previous picture boxes ( a ,b and c) to this work space they get dissappeared i mean . they get under this and beomes invisible along with that .. i mentioned . i don neend to move my orignial picture boxes to workspace .. need their instances to be created at run time ....

  21. #21
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Uh... you need to move them inside the workspace picture box first

    and don't put the drag+drop code in the _MouseMove event of the workspace control...

  22. #22

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    i m sorry .. i couldnt get .. this is the code i have right now . pictureboxes (a,b and c ) and work space picturebox ( work)


    VB Code:
    1. Private Sub a_MouseMove( _
    2.     Button As Integer, _
    3.     Shift As Integer, _
    4.     X As Single, _
    5.     Y As Single _
    6. )
    7.  
    8.  
    9. Static ssngMouseX   As Single
    10. Static ssngMouseY   As Single
    11.  
    12.     If (Button = vbLeftButton) Then
    13.         X = ((X - ssngMouseX) \ 25) * 25
    14.         Y = ((Y - ssngMouseY) \ 25) * 25
    15.              
    16.         a.Move (a.Left + X), _
    17.                       (a.Top + Y)
    18.         a.Visible = True
    19.        
    20.       Else
    21.         ssngMouseX = X
    22.         ssngMouseY = Y
    23.  
    24.     End If
    25. End Sub
    26. Private Sub b_MouseMove( _
    27.     Button As Integer, _
    28.     Shift As Integer, _
    29.     X As Single, _
    30.     Y As Single _
    31. )
    32.  
    33.  
    34. Static ssngMouseX   As Single
    35. Static ssngMouseY   As Single
    36.  
    37.     If (Button = vbLeftButton) Then
    38.         X = ((X - ssngMouseX) \ 25) * 25
    39.         Y = ((Y - ssngMouseY) \ 25) * 25
    40.              
    41.         b.Move (b.Left + X), _
    42.                       (b.Top + Y)
    43.         b.Visible = True
    44.        
    45.       Else
    46.         ssngMouseX = X
    47.         ssngMouseY = Y
    48.  
    49.     End If
    50. End Sub
    51. Private Sub c_MouseMove( _
    52.     Button As Integer, _
    53.     Shift As Integer, _
    54.     X As Single, _
    55.     Y As Single _
    56. )
    57. Static ssngMouseX   As Single
    58. Static ssngMouseY   As Single
    59.  
    60.     If (Button = vbLeftButton) Then
    61.         X = ((X - ssngMouseX) \ 25) * 25
    62.         Y = ((Y - ssngMouseY) \ 25) * 25
    63.              
    64.         c.Move (c.Left + X), _
    65.                       (c.Top + Y)
    66.         c.Visible = True
    67.        
    68.       Else
    69.         ssngMouseX = X
    70.         ssngMouseY = Y
    71.  
    72.     End If
    73. End Sub
    74.  
    75. Private Sub work_Click()
    76.  
    77. End Sub

    what should i do now ? ... considering the fact that i m total goof

  23. #23
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    In design mode, select your pictureboxes a,b,c and cut+paste them into the Work picturebox. That way, they will appear inside it.

  24. #24

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    done . .and working ..but still .... i have to work with instances of picture boxes ... and thts too inside the workspace (work) .. i only need instances to be placed at run time .......... see . i have i have three pictureboxes ( a , b and c ) .. right now placed outside workspace ( for kinda reference that user may know that what kinda connections "pictureboxes " ) he can have for further manipulation .... at run time .. i want their instance to be created ( as user click any of them ) .. and than that instance should be dragged and dropped inside that work space ( all that gridding stuff will be inside this workspace picture box ) .......


    and i m cant rate ur post .. donno y . msg prompts .. " You must spread some Reputation around before giving it to penagate again." ....

  25. #25
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Drag and drop at run time ..

    You probably can't because you don't have 50 posts yet, or haven't got any rep points yourself.

  26. #26
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    To be able to dynamically create pictureboxes you could use a control array, instead of a,b,c separate controls. However, that requires a slight modification to the event handler code.

    Assume, that your control array is named picBox, say

    VB Code:
    1. Private Sub picBox_MouseMove( _
    2.     Index As Integer
    3.     Button As Integer, _
    4.     Shift As Integer, _
    5.     X As Single, _
    6.     Y As Single _
    7. )
    8. Static ssngMouseX()   As Single
    9. Static ssngMouseY()   As Single
    10.  
    11.     If (Index > UBoundSng(ssngMouseX)) Then
    12.         ReDim Preserve ssngMouseX(UBoundSng(ssngMouseX) + 1)
    13.         ReDim Preserve ssngMouseY(UBoundSng(ssngMouseY) + 1)
    14.     End If
    15.  
    16.     If (Button = vbLeftButton) Then
    17.         X = ((X - ssngMouseX(Index)) \ 25) * 25
    18.         Y = ((Y - ssngMouseY(Index)) \ 25) * 25
    19.  
    20.         picBox(Index).Move (c.Left + X), (c.Top + Y)
    21.  
    22.       Else
    23.         ssngMouseX = X
    24.         ssngMouseY = Y
    25.  
    26.     End If
    27. End Sub
    28.  
    29. ' extra funciton for array ubounds
    30. Private Function UBoundSng(ByRef lpsaSng() As Single) As Long
    31.     On Error Resume Next
    32.         UBoundSng = UBound(lpsaSng)
    33.     If (Err.Number) Then
    34.         UBoundSng = -1
    35.     End If
    36. End Function

    Quote Originally Posted by Saint333
    and i m cant rate ur post .. donno y . msg prompts .. " You must spread some Reputation around before giving it to penagate again." ....
    You need to rep another 10 members first.

  27. #27
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Drag and drop at run time ..

    Pena can do without the Rep.. He just does it for the warm fuzzy feeling he gets when helping other people... Right pena? :-)

  28. #28
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Of course, we all do

  29. #29

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    ahaan... k i have tried to create a control array of picBox . by setting its index to value "0" ( this is how .. i think control array of picture boxes is created :$ ) ... and jus for testing i have renamed my picturebox "a" to "picBox" ..
    after this i deleted all other coding in my code and jus copy pasted what u gave me in previous post ...
    but as soon as i paste
    vb changes the color of this much portion to red

    Private Sub picBox_MouseMove( _
    Index As Integer
    Button As Integer, _
    Shift As Integer, _
    X As Single, _
    Y As Single _
    )

    and throws error like ... compile error .. expected list separator ... :$ :$ :$

  30. #30
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Ah fudge. I missed a comma

    VB Code:
    1. Private Sub picBox_MouseMove( _
    2.     Index As Integer, _
    3.     Button As Integer, _
    4.     Shift As Integer, _
    5.     X As Single, _
    6.     Y As Single _
    7. )

  31. #31

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    that error resolved ... but now after compiling . it hightlights "ssngMouseX" from this portion

    Else
    ssngMouseX = X
    ssngMouseY = Y


    as blue and throws error like ..
    compile error .. cant assign to array ...

  32. #32
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Gaah, my mistake again *shakes head*

    VB Code:
    1. ssngMouseX(Index) = X
    2. ssngMouseY(Index) = Y

    Sorry about that.

  33. #33

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    ohh comon sir .. don be sorry .. i mean ... i have been eating ur brain since noon with all my stupid questions .. don u think i should be sorry first and many times ... anyway ... its again ... i mean its compiling .. running aswell .. but .. isnt creating instances .. rather .. it also isnt following mouse curser ( as it was doing ) ... rather seems like movin a car without wheels ... kinda sticks to where it is ... and tries to keep remain close to .. i mean u should see ur self .. ..

  34. #34
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..



    Maybe I'll throw together an example. Hold on a bit.

  35. #35
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Here you go. This example lets you add and remove pictureboxes at runtime, change the size of the grid, and it even draws the grid lines for you (I thought that was quite cool myself )

    I changed the behaviour of the snap-to-grid effect so that it only applies when you release the mouse button, which is a more standard behaviour.

    Hope it helps.

    Attached Files Attached Files

  36. #36

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    46

    Re: Drag and drop at run time ..

    penagate . its awesome .. i mean .. thts exactly what i was lookin for ... excellent work .. full points .. if i wouldnt have run out of reputations before .. i must have given u buch of them .... and luckily ( or you intentionally considerin the fact that i m total goof ) didnt lock the code either ... thanks a bunch . now lemme spend some time with this to alter and mess with it .... so that i may develop something according to my design ... chaos

  37. #37
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Drag and drop at run time ..

    Always glad to help

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