?
Solved

Drag and Drop texto from a listbox to another showing the word being dragged.

Posted on 2013-12-04
3
Medium Priority
?
576 Views
Last Modified: 2013-12-04
Hello,

I have a listbox1 with some words and a second empty listbox2. I can drag any word from listbox1 and drop it to listbox2, but the cursor is a drag figure and can't see the word that I am draging to the destination.

Is it possible to do it as it was made to picture? When I drag the image, I can pic it and it float with my cursor until the destination, can I do that with words?

pic drag from this thread http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_28302574.html#a39695283

thanks
Alex
0
Comment
Question by:hidrau
3 Comments
 
LVL 19

Accepted Solution

by:
Thommy earned 1000 total points
ID: 39695643
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, jpeg, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    ListBox1: TListBox;
    ListBox2: TListBox;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Image1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure Image1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure ListBox2StartDrag(Sender: TObject; var DragObject: TDragObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TTextDragObject = class(TDragControlObjectEx)
  private
    FDragImages: TDragImageList;
    FText: String;
  protected
    function GetDragImages: TDragImageList; override;
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox2.DragMode := dmAutomatic;
  ControlStyle := ControlStyle + [csDisplayDragImage];
  Image1.ControlStyle := ListBox2.ControlStyle + [csDisplayDragImage];
end;

procedure TForm1.Image1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  ListBox1.Items.Add(ListBox2.Items[ListBox2.Tag]);
end;

procedure TForm1.Image1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept:=true;
end;


procedure TForm1.ListBox2StartDrag(Sender: TObject;
  var DragObject: TDragObject);
var
  List: TListbox absolute Sender;
begin
  DragObject := TTextDragObject.Create(List);
  if List.ItemIndex > -1 then
    TTextDragObject(DragObject).FText := List.Items[List.ItemIndex];
    List.Tag:=List.ItemIndex;
end;

function TTextDragObject.GetDragImages: TDragImageList;
var
  Bmp: TBitmap;
begin
  if FDragImages = nil then
  begin
    FDragImages := TDragImageList.Create(Control);
    Bmp := TBitmap.Create;
    try
      Bmp.Width := Bmp.Canvas.TextWidth(FText);
      Bmp.Height := Bmp.Canvas.TextHeight(FText);
      Bmp.Canvas.TextOut(0, 0, FText);
      FDragImages.Width := Bmp.Width;
      FDragImages.Height := Bmp.Height;
      FDragImages.SetDragImage(FDragImages.Add(Bmp, nil), 0, 0);
    finally
      Bmp.Free;
    end;
  end;
  Result := FDragImages;
end;


end.

Open in new window

0
 
LVL 28

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 1000 total points
ID: 39696634
My example is very similar. I use transparency in dragging text. Works as I described in another thread and Thommy did it similar too.

Thommy, small comment if I may - you didn't destroy your TDragImageList after usage.
drag.zip
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 39697035
Thanks very much everybody
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question