#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    233
    Rep Power
    8

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    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.

IMN logo majestic logo threadwatch logo seochat tools logo