Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 14th, 2012, 08:51 AM
ud2006 ud2006 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 230 ud2006 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 20 h 39 m
Reputation Power: 7
Moving listview with columns items up/down with buttons

I have a listview with columns, and I try to move a single selected item (row selected) up or down, but nothing happends, not even an error message.

The multiselect from the listview is set to False.

Here is the code I have, it does work when I don't have multiple columns:
Code:
procedure TForm1.upbtnClick(Sender: TObject);
var
  Index: integer;
  temp : TListItem;
begin
  if ListView1.Focused then
    if ListView1.SelCount>0 then
    begin
      //ListView1.ReadOnly := False;
      Index := ListView1.Selected.Index;
      if Index>0 then
      begin
        temp := ListView1.Items.Insert(Index-1);
        temp.Assign(ListView1.Items.Item[Index+1]);
        ListView1.Items.Delete(Index+1);
        // fix display so moved item is selected/focused
        ListView1.Selected := temp;
        ListView1.ItemFocused := temp;
      end;
      //ListView1.ReadOnly := True;
    end; 
end;

procedure TForm1.downbtnClick(Sender: TObject);
var
  Index: integer;
  temp : TListItem;
begin
  if ListView1.Focused then
    if ListView1.SelCount>0 then
    begin
      //ListView1.ReadOnly := False;
      Index := ListView1.Selected.Index;
      if Index<ListView1.Items.Count then
      begin
        temp := ListView1.Items.Insert(Index+2);
        temp.Assign(ListView1.Items.Item[Index]);
        ListView1.Items.Delete(Index);
        // fix display so moved item is selected/focused
        ListView1.Selected := temp;
        ListView1.ItemFocused := temp;
      end;
        //ListView1.ReadOnly := True;
    end;
end;


Thanks

Reply With Quote
  #2  
Old November 14th, 2012, 11:20 AM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 255 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 2 m 15 sec
Reputation Power: 5
This should work.
Code:
procedure TForm1.upbtnClick(Sender: TObject);
var
  Index: integer;
  SelectedItem : TListItem;
begin
   if ListView1.SelCount > 0 then
   begin
      Index := ListView1.Selected.Index;
      SelectedItem := ListView1.Selected;
      if Index > 0 then
      begin
         ListView1.Items.BeginUpdate;
         try
            ListView1.Items.Insert(Index-1).Assign(SelectedItem);
            ListView1.Items.Delete(Index+1);
            ListView1.Selected := ListView1.Items[Index-1];
         finally
            ListView1.Items.EndUpdate;
         end;
      end;
   end;
end;

procedure TForm1.downbtnClick(Sender: TObject);
var
  Index: integer;
  SelectedItem : TListItem;
begin
   if ListView1.SelCount > 0 then
   begin
      Index := ListView1.Selected.Index;
      SelectedItem := ListView1.Selected;
      if Index < (ListView1.Items.Count - 1) then
      begin
         ListView1.Items.BeginUpdate;
         try
            ListView1.Items.Insert(Index+2).Assign(SelectedItem);
            ListView1.Items.Delete(Index);
            ListView1.Selected := ListView1.Items[Index+1];
         finally
            ListView1.Items.EndUpdate;
         end;
      end;
   end;
end;


Also, I removed the "If ListView1.Focused then" - that line always evaluated false, even with the listview focused...

Last edited by majlumbo : November 14th, 2012 at 11:45 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Moving listview with columns items up/down with buttons

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap