Using Drag and Drop in a Window

About this chapter

This chapter describes how to make applications graphical by dragging and dropping controls.

About drag and drop

Drag and drop allows users to initiate activities by dragging a control and dropping it on another control. It provides a simple way to make applications graphical and easy to use. For example, in a manufacturing application you might allow the user to pick parts from a bin for an assembly by dragging a picture of the part and dropping it in the picture of the finished assembly.

Drag and drop involves at least two controls: the control that is being dragged (the drag control) and the control to which it is being dragged (the target). In PowerBuilder, all controls except drawing objects (lines, ovals, rectangles, and rounded rectangles) can be dragged.

Automatic drag mode

When a control is being dragged, it is in drag mode. You can define a control so that PowerBuilder puts it automatically in drag mode whenever a Clicked event occurs in the control, or you can write a script to put a control into drag mode when an event occurs in the window or the application.

Drag icons

When you define the style for a draggable object in the Window painter, you can specify a drag icon for the control. The drag icon displays when the control is dragged over a valid drop area (an area in which the control can be dropped). If you do not specify a drag icon, a rectangle the size of the control displays.

Drag events

Window objects and all controls except drawing objects have events that occur when they are the drag target. When a dragged control is within the target or dropped on the target, these events can trigger scripts. The scripts determine the activity that is performed when the drag control enters, is within, leaves, or is dropped on the target.

Drag-and-drop properties, events, and functions

Drag-and-drop properties

Each PowerBuilder control has two drag-and-drop properties:

  • DragAuto

  • DragIcon

The DragAuto property

DragAuto is a boolean property.

Value

Meaning

TRUE

When the object is clicked, the control is placed automatically in drag mode

FALSE

When the object is clicked, the control is not placed automatically in drag mode; you have to put the object in drag mode manually by using the Drag function in a script


To specify automatic drag mode for a control in the Window painter:

  1. Select the Other property page in the Properties view for the control.

  2. Check the Drag Auto check box.

The DragIcon property

Use the DragIcon property to specify the icon you want displayed when the control is in drag mode. The DragIcon property is a stock icon or a string identifying the file that contains the icon (the ICO file). The default icon is a box the size of the control.

When the user drags a control, the icon displays when the control is over an area in which the user can drop it (a valid drop area). When the control is over an area that is not a valid drop area (such as a window scroll bar), the No-Drop icon displays.

To specify a drag icon:

  1. Select the Other property page in the Properties view for the control.

  2. Choose the icon you want to use from the list of stock icons or use the Browse button to select an ICO file and click OK.

Creating icons

To create icons, use a drawing application that can save files in the Microsoft Windows ICO format.

Drag-and-drop events

There are six drag-and-drop events.

Event

Occurs

BeginDrag

When the user presses the left mouse button in a ListView or TreeView control and begins dragging

BeginRightDrag

When the user presses the right mouse button in a ListView or TreeView control and begins dragging

DragDrop

When the hot spot of a drag icon (usually its center) is over a target (a PowerBuilder control or window to which you drag a control) and the mouse button is released

DragEnter

When the hot spot of a drag icon enters the boundaries of a target

DragLeave

When the hot spot of a drag icon leaves the boundaries of a target

DragWithin

When the hot spot of a drag icon moves within the boundaries of a target


Drag-and-drop functions

Each PowerBuilder control has two functions you can use to write scripts for drag-and-drop events.

Function

Action

Drag

Starts or ends the dragging of a control

DraggedObject

Returns the control being dragged


For more information about these events and functions, see PowerScript Reference.

Identifying the dragged control

To identify the type of control that was dropped, use the source argument of the DragDrop event.

This script for the DragDrop event in a picture declares two variables, then determines the type of object that was dropped:

CommandButton lcb_button
StaticText lst_info
 
IF source.TypeOf() = CommandButton! THEN
      lcb_button = source
      lcb_button.Text = "You dropped a Button!"
ELSEIF source.TypeOf() = StaticText! THEN
      lst_info = source
      lst_info.Text = "You dropped the text!"
END IF

Using CHOOSE CASE

If your window has a large number of controls that can be dropped, use a CHOOSE CASE statement.