Forum Discussion

aish's avatar
aish
New Member
8 years ago

Drag and Drop without using X, Y coordinates or Position

Hi
We are trying to drag and drop an object from one window to other. Major Constraint here is that we should not use X and Y co-ordinates. Is there any way to do this? Please help.

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The "Drag" method common on any on screen objects in TestComplete only operates using X,Y co-ordinates for the start position and the end position. So, unfortunately, there's no way to do what you are asking "out of the box".

    If you know the objects both of the source and the destination and the screen-coordinates of them both, it should be something like

     

    MySourceObject.Drag(1,1,MySourceObject.Top - MyDestinationObject.Top, MySourceObject.Left - MyDestinationObject.Left)

    The first two parameters say to just grab your object at point 1,1 relative to the object.  The second parameters do a bit of basic math to calculate the X distance and the Y distance for your travel.  

  • We get around this by dynamically capturing the X,Y coordinates from the desktop. Click on the item or location you want to drag from and grab the X, Y coordinates (relative to the desktop).

     

    ax = Sys["Desktop"]["MouseX"];
    ay = Sys["Desktop"]["MouseY"];

    Then click on the item or location you want to drag to and grab the X, Y coordinates (relative to the desktop).

     

    bx = Sys["Desktop"]["MouseX"];
    by = Sys["Desktop"]["MouseY"];

    Calculate the differences.

     

    XDiff = bx-ax
    YDiff = by-ay

    Then perform the drag operation (from coordinates 50, 35 in the grid control sample below).

     

    myGrid["Drag"](50, 35, XDiff, YDiff, skAlt)

     

    Good luck!

     

    • Colin_McCrae's avatar
      Colin_McCrae
      Community Hero

      Yeah.

       

      I think what you actually mean is you don't want to use hard-coded co-ordinates throughout.

       

      Which is fine.

       

      But you have to use co-ordinates to tell it how far to move something. That's the nature of drag and drop and there is no way round that. But, as mentioned above, you can do everything relative to the objects starting point, and all the information can be captured and calculated at run time. The only thing you have to add in to the mix is how far (X and Y distances, rather than co-ordinates) the object has to move, and then factor that into your working at run time.

       

      I have a generic "drag this object" (which is designed to only work with certain object types on certain screens in my application where drag and drop is required). Everything is calculated by the function. In the input data, the user simply has to specify which object they want to move, and how far they want to move it. The function then finds the object and works out all the rest of the numbers itself.

       

      Not that tricky really.