Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to exchange two items in a TListView?

Posted on 1999-07-29
6
Medium Priority
?
310 Views
Last Modified: 2011-09-20
Hello.
I'm supporting drag 'n' drop within the same TListView (Style is vsReport). I set DragMode to dmAutomatic, then I check if what is going to be dropped is an item of the same TListView in this way:

procedure TMainForm.MyListViewDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  if TListView(Source).Name = 'MyTListView' then Accept := True
  else Accept := False
end;

Then, I would like to do this: when the user drags an item over another, their position in the TListView must be exchanged.
In the OnDragDrop event I write something like this:

procedure TMainForm.MyListViewDragDrop(Sender, Source: TObject; X,
  Y: Integer);
var ItemSource, TmpItem, ItemTarget: TListItem;
begin
  {the item being dragged is the selected one}
  TmpItem := TListView(Sender).Selected;
  {copy the whole selected item to ItemSource}
  Move(TmpItem, ItemSource, SizeOf(TListItem));
  {the target item, instead, is the one which is under the cursor when dropping}
  ItemTarget := TListView(Sender).GetItemAt(X, Y);
  if ItemTarget <> nil then
    with TListView(Sender) do
      begin
        Items.BeginUpdate;
        {at the position of the source put the target item}
        Items.Item[Selected.Index].Assign(ItemTarget);
      {viceversa, at the position of the target, put the source}
        Items.Item[ItemTarget.Index].Assign(ItemSource);
        Items.EndUpdate;
      {if I don't put the following line, checkboxes appear in MyListView... why!?!?!?}
        CheckBoxes := False
      end;
end;

But this code does not work correctly: when I drag 'n' drop from and to MyListView, I get two copies of the target item, instead of the exchange of the source with the target... Suppose I have:
Item1
Item2
I try to drag Item1 over Item2, I drop it and then the result is:
Item2
Item2
instead of:
Item2
Item1

How do I have to proceed?
Thanks in advance.
Mauro
0
Comment
Question by:mauromol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1391801
hi mauromol,

this will do it

procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var Item : TListItem;
begin
  Item := TListItem.Create(ListView1.Items);
  try
    Item.Assign(Listview1.GetItemAt(x,y));
    Listview1.GetItemAt(x,y).Assign(ListView1.Selected);
    ListView1.Selected.Assign(Item);
  finally
    Item.Free;
  end;
end;

procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Sender = Source) and
            (Listview1.GetItemAt(x,y) <> Nil) and
            (Listview1.GetItemAt(x,y) <> ListView1.Selected);
end;


meikl
0
 

Author Comment

by:mauromol
ID: 1391802
Sorry kretzschmar,
this does not work properly: it sometimes does, but sometimes the exchange is weird... Suppose I have:
Item1
Item2
Item3
and I drag Item3 over Item2, sometimes this happens:
Item3
Item2
Item2
I really don't know why...

I use Delphi 4 (if this can help...)

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1391803
? hmm, works fine for me
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mauromol
ID: 1391804
Hi kretzschmar,
you know what? If I set CheckBoxes property to True and leave it True, your code works perfectly.... why!?!?!
I swear I didn't changed a single line of code!
Maybe there's a bug in Delphi 4... Another problem is that after I exchange two items, I lose the state of each check box (all the check boxes are unchecked except for the one of the source item...). Hmmmm.....

Anyway, I can accept your answer, but please re-propose it. And, of course, if you have any suggestion about how to keep check boxes unaltered (or to easily fix them after the exchange has taken place) please tell me! :-)
Bye,
Mauro.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1391805
hi mauromol,

i've done some tests with the checkboxes in the listview (never done before) and you've right, it seems not properly work, in all my done variations the state of the checkbox is not switched correctly. don't know why.

if the checkbox and drag&drop a must, may it possible to you to do it with a TCheckListBox ?

meikl
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 300 total points
ID: 1391806
hi mauromol,

>Anyway, I can accept your answer
well, then i post an answer now,
but i have no suggestion how to keep check boxes unaltered,
seems not work properly the checkbox-feature in a tlistview.

see at my last comment, maybe its an alternate

meikl
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question