Skip to main content

Example: TcxCustomCheckListBox.MoveSelection

  • 2 minutes to read

Consider an example of implementing drag-and-drop from the source check list box to another check list box control. During drag-and-drop, selected items are moved from one control into the destination control using the TcxCustomCheckListBox.MoveSelection method.

For the source check list box, we set the DragMode property to dmManual and start drag-and-drop in the OnMouseDown event.

procedure TForm1.cxCheckListBoxSourceMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  (Sender as TcxCustomCheckListBox).BeginDrag(False);
end;

The BeginDrag method starts drag-and-drop when the user presses the control with the mouse button and then moves the mouse by holding the button down. BeginDrag creates a drag object that is passed as a Source parameter in the OnDragOver and OnDragDrop events of the destination control. The drag object refers to the control which initiates drag-and-drop.

For the destination check list box, you need to handle the OnDragOver and OnDragDrop events which are fired when the user moves and drops the dragged object respectively.

In our OnDragOver event handler, we need to enable dropping the dragged object if drag-and-drop was started in another checklist box control. First we determine the type of the dragged object. If drag-and-drop was initiated by a TcxControl descendant, the drag object specifies an instance of the TcxDragControlObject class. Its Control property refers to the control itself.

procedure TForm1.cxCheckListBoxDestDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  if Source is TcxDragControlObject then
    with TcxDragControlObject(Source) do
      Accept := Control is TcxCustomCheckListBox;
end;

In the OnDragDrop event, we get the control that initiates the drag-and-drop operation and call its TcxCustomCheckListBox.MoveSelection method provided that the control represents a TcxCustomCheckListBox object.

procedure TForm1.cxCheckListBoxDestDragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
  if Source is TcxDragControlObject then
    with TcxDragControlObject(Source) do
      if Control is TcxCustomCheckListBox then
        (TcxCustomCheckListBox(Control)).MoveSelection(Sender as TcxCustomCheckListBox);
end;