Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to rearrange items within a TListBox Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
27-Aug-02
Category
VCL-General
Language
Delphi 2.x
Views
37
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Can someone point me to a document on how to drag items around (reposition) within 
a TListbox?

Answer:

Solve 1:

It is easier than you might think. Set the DragMode property to dmAutomatic, then 
provide these event-handlers for OnDragDrop and OnDragOver:
1   
2   procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
3     State: TDragState; var Accept: Boolean);
4   begin
5     Accept := (Sender = Source);
6   end;
7   
8   procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
9   var
10    DropIndex: Integer;
11  begin
12    DropIndex := ListBox1.ItemAtPos(Point(X, Y), True);
13    ListBox1.Items.Exchange(ListBox1.ItemIndex, DropIndex);
14  end;


Solve 2:

There is no build-in method. Try that:

15  
16  procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
17    State: TDragState; var Accept: Boolean);
18  begin
19    Accept := Sender is TListBox;
20  end;
21  
22  procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
23  var
24    iTemp: integer;
25    ptTemp: TPoint;
26    szTemp: string;
27  begin
28    { change the x, y coordinates into a TPoint record }
29    ptTemp.x := x;
30    ptTemp.y := y;
31    { Use a while loop instead of a for loop due to items possible being removed
32     from listboxes this prevents an out of bounds exception }
33    iTemp := 0;
34    while iTemp <= TListBox(Source).Items.Count - 1 do
35    begin
36      { look for the selected items as these are the ones we wish to move }
37      if TListBox(Source).selected[iTemp] then
38      begin
39        { use a with as to make code easier to read }
40        with Sender as TListBox do
41        begin
42          { need to use a temporary variable as when the item is deleted the indexing 
43  will change }
44          szTemp := TListBox(Source).items[iTemp];
45          { delete the item that is being dragged  }
46          TListBox(Source).items.Delete(iTemp);
47          { insert the item into the correct position in the listbox that it was 
48  dropped on }
49          items.Insert(itemAtPos(ptTemp, true), szTemp);
50        end;
51      end;
52      inc(iTemp);
53    end;
54  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC