| Home | Previous Lesson: Drag-n-Drop Attributes Next Lesson: Implementing the Drag-n-Drop Functionality |
There are couple of functions related to the drag-n-drop. One is Drag() and the other one is DraggedObject().
You use the drag() function to start, cancel/stop the drag mode. For example, you may want to start drag mode when the mouse is moving while the left mouse button is depressed.
IF keydown( KeyLeftButton! ) THEN
This.Drag( Begin! )
END IF
The above code is for the ue_mouse_move event for a DataWindow control. This event is mapped to pbm_mousemove event id. We are checking whether left mouse button is depressed by calling KeyDown() function. If so, we are turning on the drag mode.
Dropping on the target object automatically ends the drag mode.
In the target object, you can call DraggedObject() function to find out the object that is dragged. Then you can call TypeOf() and ClassName() functions to find out the object type and the class name.
If DraggedObject().TypeOf() = DataWindow! Then
If DraggedObject().ClassName() = "dw_left" Then
//Code
End If
End if
In the above code, we are checking whether the dragged object is a DataWindow or not. If it's a DataWindow, we are checking whether its name is dw_left or not.
The above code won't work if you are using versions prior to 5.0. Because, we are using nested functions. You need to write the following code instead of the above:
DragObject l_DragObject
l_DragObject = DraggedObject()
If l_DragObject.TypeOf() = DataWindow! Then
If l_DragObject.ClassName() = "dw_left" Then
// Code
End If
End If
In the nested code listing, we are skipping one step of storing the object in a local variable. The function DraggedObject() returns an object that is dragged. In the first code fragment, we are expecting that TypeOf() function is available at the object returned by the DraggedObject(). For example, say, DraggedObject() returns a DataWindow control, TypeOf() function is available at the DataWindow control.
TypeOf() and ClassName() functions are available at all PowerBuilder objects, so, there won't be any problem and both of the above code fragments are right. Do you agree?
The answer is Yes and No. If you are writing this code to the DragDrop event and if you are sure that this DragDrop is not being called from anywhere in the program, then the above code is right. But, if you are calling the DragDrop event from somewhere in the application or if you are writing this code as a function and being called from places other than DragDrop event, you are in trouble. Any Guesses?
To illustrate the problem, we wrote the first listing of the code in the clicked event of a CommandButton and ran the application. The above picture is the result of clicking on that CommandButton. What happened?
When we click on the CommandButton, we are not dropping any object on CommandButton. So, the DraggedObject() returns null value. We are calling TypeOf() function for a null object. That's why the above error.
Be careful while calling the nested functions. Make sure the function returns proper object and the nested function is available at the returned object. It's hard to debug this type of code also.
Even the second code fragment is not the perfect code. You need to check for the valid object before the IF function:
If NOT IsValid( l_DragObject ) Then Return -1
| Home | Previous Lesson: Drag-n-Drop Attributes Next Lesson: Implementing the Drag-n-Drop Functionality |