## Forum Discussion

sonya_m
SmartBear Alumni (Retired)
4 years ago
Solved

# [TechCorner Challenge #2] Dragging One Element to Another

Hi Community! Here's one more interesting challenge for you to complete!

Drag and drop is quite a simple task that is performed in TestComplete by using the Drag action. However, it might be that the destination changes its position on the screen, so you need to recalculate the destination coordinates every time.

Task: Create a function that takes two objects as parameters and drags the first object to the center of the second one.

Difficulty:

Have fun!ğŸ™‚

• Task: Create a function that takes two objects as parameters and drags the first object to the center of the second one.

This is a solution created for [TechCorner Challenge #2]

In Javascript&colon;

``````function dragCalculatedDistance(startObject, destinationObject) {
var startPoint, targetPoint, dragX, dragY;
// Drag from the center of object A to the center of object B

startPoint = startObject.WindowToScreen(startObject.Width/2, startObject.Height/2);
targetPoint = destinationObject.WindowToScreen(destinationObject.Width/2, destinationObject.Height/2);

dragX = targetPoint.X - startPoint.X;
dragY = targetPoint.Y - startPoint.Y;

startObject.Drag(-1, -1, dragX, dragY);
}``````

### 2 Replies

• tristaanogre
Esteemed Contributor

Task: Create a function that takes two objects as parameters and drags the first object to the center of the second one.

This is a solution created for [TechCorner Challenge #2]

In Javascript&colon;

``````function dragCalculatedDistance(startObject, destinationObject) {
var startPoint, targetPoint, dragX, dragY;
// Drag from the center of object A to the center of object B

startPoint = startObject.WindowToScreen(startObject.Width/2, startObject.Height/2);
targetPoint = destinationObject.WindowToScreen(destinationObject.Width/2, destinationObject.Height/2);

dragX = targetPoint.X - startPoint.X;
dragY = targetPoint.Y - startPoint.Y;

startObject.Drag(-1, -1, dragX, dragY);
}``````