Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Example: TcxCustomGridView.OnStartDrag

  • 3 minutes to read

This example shows how to create a custom drag object for drag-and-drop. The drag object will provide a drag image to display while dragging records of the tvItems View. If a user starts dragging within the View with the multi-selection feature enabled (the OptionsSelection.MultiSelect property is set to True), the drag image will display information on the number of selected records:

If the multi-selection feature is disabled, the drag image will look as follows:

To provide a custom drag object, you need to:

The following is an example of implementing a custom drag object when dragging from the tvItems View. The OnStartDrag event handler just creates a drag object of the TMyDragControlObject class and assigns it to the DragObject parameter.

procedure TForm1.tvItemsStartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  DragObject := TMyDragControlObject.Create(Sender as TcxGridSite);
end;

The TMyDragControlObject class overrides the GetDragImages method inherited from TDragControlObject. This method should return a list containing the image to be displayed while dragging. The drag image is created by the private CreateImageList method, which will display information regarding the selected/focused records of the View in the form shown above.

TMyDragControlObject = class(TDragControlObjectEx)
  private
    FImageList: TImageList;
    procedure CreateImageList;
  protected
    function GetDragImages: TDragImageList; override;
  public
    constructor Create(AControl: TControl); override;
    destructor Destroy; override;
  end;
//...
{ TMyDragControlObject }
constructor TMyDragControlObject.Create(AControl: TControl);
begin
  inherited Create(AControl);
end;
procedure TMyDragControlObject.CreateImageList;
var
  AView: TcxCustomGridTableView;
  s: string;
  ABmp: TBitmap;
begin
  if (FImageList = nil) and (Control is TcxGridSite)then
  begin
    //Get the current View
    AView := TcxCustomGridTableView(TcxGridSite(Control).GridView);
    //Prepare the text to display within the drag image
    if AView.OptionsSelection.MultiSelect then
      s := '      Dragging ' + IntToStr(AView.Controller.SelectedRecordCount) + ' selected records from the ' + AView.Name + ' View'
    else
      s := '      Dragging the focused record from the ' + AView.Name + ' View';
    //Create the drag image
    ABmp := TBitmap.Create();
    try
      with ABmp.Canvas do
      begin
        ABmp.Width := TextWidth(s);
        ABmp.Height := TextHeight(s);
        TextOut(0, 0, s);
      end;
      FImageList := TImageList.CreateSize(ABmp.Width, ABmp.Height);
      FImageList.AddMasked(ABmp, clNone);
    finally
      ABmp.Free();
    end;
  end;
end;
destructor TMyDragControlObject.Destroy;
begin
  if FImageList <> nil then
    FImageList.Free;
  inherited;
end;
function TMyDragControlObject.GetDragImages: TDragImageList;
begin
  if FImageList = nil then
    CreateImageList();
  Result := FImageList;
end;