Articles   Members Online: 3
-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 do I do drag and drop in 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-Dec-02
Category
VCL-General
Language
Delphi 2.x
Views
118
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How do I do drag and drop in a TListbox?

Answer:

Adding Drag and Drop facilities to a listbox is a matter of checking to see if 
there is an item under the mouse pointer in the MouseDown event, and if so save the 
item text and the index number to variables. Then check the MouseUp event to see if 
there is a different item under the mouse. If so, delete the old item and insert a 
copy of it in the new position.

Firstly, add three variables to the private section:

1   { Private declarations }
2     Dragging: Boolean;
3     OldIndex: Integer;
4     TempStr: string;
5   
6   //Then add the following code to the MouseUp and MouseDown events:
7   
8   procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton;
9       Shift: TShiftState; X, Y: Integer);
10  var
11      Index: integer;
12  begin
13      if Dragging then
14          begin
15            Index := ListBox1.ItemAtPos(point(x, y), true);
16          if(Index > -1) and (Index <> OldIndex) then
17              begin
18                ListBox1.Items.Delete(OldIndex);
19              ListBox1.Items.Insert(Index, TempStr);
20              ListBox1.ItemIndex := Index;
21            end;
22        end;
23      Dragging := false;
24  end;
25  
26  procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
27      Shift: TShiftState; X, Y: Integer);
28  var
29      Index: integer;
30  begin
31      Index := ListBox1.ItemAtPos(point(x, y), true);
32      if Index > -1 then
33          begin
34            TempStr := ListBox1.Items[Index];
35          OldIndex := Index;
36          Dragging := true;
37        end;
38  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