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 exchange items in TListView 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
18-Sep-03
Category
VCL-General
Language
Delphi 3.x
Views
72
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Mike Shkolnik

Exchange items in TListView

Answer:

Today I want to describe how you may exchange some items in standard TListView. For 
example, you have 5 items and want to swap positions for first and third items

Problem that standard TListView component haven't such method and you must realize 
it yourself.

We remember that the standard way from old Pascal times (for numbers) is:

1   procedure Swap(X, Y: Integer);
2   var
3     s: Integer;
4   begin
5     s := X;
6     X := Y;
7     Y := X
8   end;


Something similar we can do with TListItem too. But just to save all strings 
(caption+sub items) somewhere is not enough because TListItem class have a lot of 
other information (image indexes, pointer as Data, etc)

So correct way is to use Assign method:
9   
10  procedure ExchangeItems(lv: TListView; const i, j: Integer);
11  var
12    tempLI: TListItem;
13  begin
14    lv.Items.BeginUpdate;
15    try
16      tempLI := TListItem.Create(lv.Items);
17      tempLI.Assign(lv.Items.Item[i]);
18      lv.Items.Item[i].Assign(lv.Items.Item[j]);
19      lv.Items.Item[j].Assign(tempLI);
20      tempLI.Free;
21    finally
22      lv.Items.EndUpdate
23    end;
24  end;


So structure is a same as in our sample for Integer. All what we added are 
BeginUpdate and EndUpdate (just allow to reduce a flickering)

So if you want to exchange items in any ListView, just call this procedure...

			
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