Link to home
Start Free TrialLog in
Avatar of JackKuti
JackKuti

asked on

Dragging an item

I want to click item in the ListBox and then drag the item somewhere on other component (eg. other ListBox), but I want the item still to appear under the cursor while draging. To be more exact, I want to drag name of font from the font list and drop it to some text in other component and by this to change font of the text.  How can be this effect achieved?
Avatar of atul_parmar
atul_parmar
Flag of India image

e.g. ListBox1 contains the font names then
Set the following
  1. ListBox1.Drag Mode = dgAutomatic;
  2. ListBox1.Tag := -1; // this will track the item to drag

in the OnMouseDown of ListBox1 write the following line

  ListBox1.Tag := ListBox1.ItemAtPos(Point(X, Y), True);

in the OnDragOver event of other control put the following line

Accept := ListBox1.Tag > -1;

in the OnDragDrop event of other control put the following code

  if ListBox1.Tag > -1 then
  begin
   Edit1.Font.Name := ListBox1.Items.Strings[ListBox1.Tag]; // assuming that the item is being dragged to edit box
   ListBox1.Tag := -1; // reset the dragged item
  end;
Avatar of JackKuti
JackKuti

ASKER

Thanks, font are changed perfectly, but still, I´d like to achieve the effect of appearing of font name under the cursor while draging it from ListBox untill it is dropped down. This is not necessary for the application to work but its nice.  
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your code. And don´t you know how to remove the crossed ring being displayed while dragging?
Are you sure you want to do that? Its the only UI cue to the user when a  drop operation cannot be performed.

Russell

////////////////////////////////////////////////////////////////////////////////
//   TTextDrag
////////////////////////////////////////////////////////////////////////////////
type
  TTextDrag         =  class(TDragObject)
  private
     // Private declarations
     FDragImage:    TDragImageList;
     FText:         String;
  protected
     // Protected declarations
     function       GetDragCursor(Accepted: Boolean; X, Y: Integer): TCursor; override; // ADD
     function       GetDragImages: TDragImageList; override;
  public
     // Public declarations
     constructor    Create;
     destructor     Destroy; override;
     procedure      UpdateDragImage(Font: TFont; Text: String);
     property       Text: String read FText;
  end;

 etc....

implementation
{$R *.DFM}

function TTextDrag.GetDragCursor(Accepted: Boolean; X, Y: Integer): TCursor;
begin

  // Perform inherited
  result:=inherited GetDragCursor(Accepted, X, Y);

  // Check accepted = false. You can also remove this check and always return zero
  // which will make it use the default cursor
  if not(Accepted) then result:=0;

end;

Trying if it looks better without the ring, if it won´t  the ring will be returned. Many thanks to you!