Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Moving items in a TListBox using the mouseGo to Christophe Geers's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
A quick example on how to move items in a TListBox using the mouse
Product:
Delphi all versions
Category:
GUI
Skill Level:
Scoring:
Last Update:
05/07/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet TListBox ListBox drag drop items move mouse playlist winamp dragmode
Times Scored:
1
Visits:
3717
Uploader: Christophe Geers
Company: ANDROME nv
Reference: N/A
 
Question/Problem/Abstract:
This little example shows you how to move items in a listbox using the mouse. You could compare this too the way one moves songs in a winamp playlist.
Answer:



NOTE: This is just a small example that shows you this effect.

The following steps have to be followed in order to obtain a working example:

1. Set the dragmode property of the TListBox to dmAutomatic.

Next, you'll have to provide two event handler for the DragDrop and the
DragOver event of the TListBox.

2. Put the following code in the DragOver event handler.

procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
var
  DropIndex: Integer;
  TempStr  : String;
begin
  with TListBox(Sender) do
  begin
    DropIndex := ItemAtPos(Point(X,Y), True);
    if (DropIndex > -1) and (DropIndex <> ItemIndex) then
    begin
      TempStr          := Items[DropIndex];
      Items[DropIndex] := Items[ItemIndex];
      Items[ItemIndex] := TempStr;
      ItemIndex        := DropIndex;
     end;
  end;
end;

3. And finally put this little of code in the DragDrop event handler.

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  DropIndex: Integer;
begin
  DropIndex := TListBox(Sender).ItemAtPos(Point(X,Y),False);
  with TListBox(Source) do
  begin
    TListBox(Sender).Items.Insert(DropIndex, Items[ItemIndex]);
    Items.Delete(ItemIndex);
  end;
end;

Voila, there you go. You should be able now to move items in a listbox
using the mouse. This code also shows the item moving along through the
other items while you drag it. The code can be easily adjusted to prevent
this from happening. It's all pretty straight forward so I didn't bother
to comment the code.

Regards,

Chris

Tested with Delphi 6 Professional on WinXP Professional)





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
What about objects
    Magnus Flysjö (May 7 2003 2:35PM)

You should add support for moving the object data in the TStrings as well, not just only the string...

Respond

RE: What about objects
Dan Mincu (May 4 2004 5:51PM)

// fixing bug that allows to drag items between TListBox instances
// support for objects added

procedure TArrangeItemsDialog.ListBox1DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
var
  DropIndex  : Integer;
  TempStr    : String;
  TempObject : TObject;
begin
  Accept := Sender = Source;
  if Accept then
    with TListBox(Sender) do
    begin
      DropIndex := ItemAtPos(Point(X,Y), True);
      if (DropIndex > -1) and (DropIndex <> ItemIndex) then
      begin
        TempStr                  := Items[DropIndex];
        TempObject               := Items.Objects[DropIndex];
        Items[DropIndex]         := Items[ItemIndex];
        Items.Objects[DropIndex] := Items.Objects[ItemIndex];
        Items[ItemIndex]         := TempStr;
        Items.Objects[ItemIndex] := TempObject;
        ItemIndex                := DropIndex;
      end;
  end;
end;


procedure TArrangeItemsDialog.ListBox1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  DropIndex: Integer;
begin
  DropIndex := TListBox(Sender).ItemAtPos(Point(X,Y),False);
  with TListBox(Source) do
  begin
    TListBox(Sender).Items.InsertObject(DropIndex, Items[ItemIndex], TListBox(Sender).Items.Objects[ItemIndex]);
    Items.Delete(ItemIndex);
  end;
end;
Respond

RE: RE: What about objects
Edwin (Jul 22 2004 6:24PM)

and how about 'multi-select'??
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)