Solved

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

Posted on 2013-12-04
3
539 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 250 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 26

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 120
Comparing dates in Delphi. Greater than/ Less than 3 117
creating threads in delphi 1 82
how to update exe applicatio from internet ? 6 67
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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now