Recent

Author Topic: listview move item up or down  (Read 3783 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
listview move item up or down
« on: July 22, 2017, 07:43:59 am »
hi my friends, I have found this code for move items up or down in listview, But when I move to the beginning or the end shows error, help me please.  :(

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  9.   Buttons;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     BitBtn1: TBitBtn;
  17.     BitBtn2: TBitBtn;
  18.     ListView1: TListView;
  19.     procedure BitBtn1Click(Sender: TObject);
  20.     procedure BitBtn2Click(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     procedure ExchangeItems(lv: TListView; const i, j: Integer);
  26.     { public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure tform1.ExchangeItems(lv: TListView; const i, j: Integer);
  39. var
  40.   tempLI: TListItem;
  41.   enumerando: integer;
  42. begin
  43. ListView1.MultiSelect:=false;
  44.  
  45.   lv.Items.BeginUpdate;
  46.   try
  47.     tempLI := TListItem.Create(lv.Items);
  48.     tempLI.Assign(lv.Items.Item[i]);
  49.     lv.Items.Item[i].Assign(lv.Items.Item[j]);
  50.     lv.Items.Item[j].Assign(tempLI);
  51.     tempLI.Free;
  52.  
  53.   finally
  54.     lv.Items.EndUpdate
  55.   end;
  56.   lv.Items.Item[j].Selected:= True;
  57.   lv.SetFocus;
  58.   ListView1.MultiSelect:=true;
  59.  
  60.  
  61.         begin // volviendo a enumerar después de borrar
  62.         for enumerando := 0 to ListView1.Items.Count-1 do
  63.         ListView1.Items[enumerando].Caption := IntToStr(enumerando+1);
  64.         end;
  65. end;
  66.  
  67. procedure TForm1.FormCreate(Sender: TObject);
  68. var
  69.   enumerando: integer;
  70. begin
  71.     with Form1.ListView1.Items.Add do
  72.   begin
  73.   Caption:= '';
  74.   SubItems.Add('erick');
  75.   SubItems.Add('caceres');
  76.   SubItems.Add('peña');
  77.   end;
  78.  
  79.     with Form1.ListView1.Items.Add do
  80.   begin
  81.   Caption:= '';
  82.   SubItems.Add('juan');
  83.   SubItems.Add('galvez');
  84.   SubItems.Add('soto');
  85.   end;
  86.  
  87.       with Form1.ListView1.Items.Add do
  88.   begin
  89.   Caption:= '';
  90.   SubItems.Add('luis');
  91.   SubItems.Add('flores');
  92.   SubItems.Add('layme');
  93.   end;
  94.  
  95.     with Form1.ListView1.Items.Add do
  96.   begin
  97.   Caption:= '';
  98.   SubItems.Add('leo');
  99.   SubItems.Add('allca');
  100.   SubItems.Add('gonzales');
  101.   end;
  102.  
  103.         begin // volviendo a enumerar después de borrar
  104.         for enumerando := 0 to ListView1.Items.Count-1 do
  105.         ListView1.Items[enumerando].Caption := IntToStr(enumerando+1);
  106.         end;
  107. end;
  108.  
  109. procedure TForm1.BitBtn1Click(Sender: TObject);  // MOVE ITEM UP
  110. begin
  111.   ExchangeItems(ListView1,ListView1.Selected.Index,ListView1.Selected.Index-1);
  112. end;
  113.  
  114. procedure TForm1.BitBtn2Click(Sender: TObject);  // MOVE ITEM DOWN
  115. begin
  116.   ExchangeItems(ListView1,ListView1.Selected.Index,ListView1.Selected.Index+1);
  117. end;
  118.  
  119. end.



PD: Attached image and project.
« Last Edit: July 22, 2017, 07:47:26 am by Ericktux »

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: listview move item up or down
« Reply #1 on: July 22, 2017, 11:31:43 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ButtonDownClick(Sender: TObject);
  2. var
  3.   Sel: TListItem;
  4. begin
  5.   Sel := ListView1.Selected;
  6.   if Assigned(Sel) then
  7.     if Sel.Index + 1 < ListView1.Items.Count then
  8.       ListView1.Items.Exchange(Sel.Index, Sel.Index + 1);
  9. end;
  10.  
  11. procedure TForm1.ButtonUpClick(Sender: TObject);
  12. var
  13.   Sel: TListItem;
  14. begin
  15.   Sel := ListView1.Selected;
  16.   if Assigned(Sel) then
  17.     if Sel.Index - 1 >= 0 then
  18.       ListView1.Items.Exchange(Sel.Index, Sel.Index - 1);
  19. end;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: listview move item up or down
« Reply #2 on: July 22, 2017, 12:18:58 pm »
Here's a complete project.

M+AUDIO

  • New Member
  • *
  • Posts: 48
Re: listview move item up or down
« Reply #3 on: July 22, 2017, 01:13:39 pm »
Hi,
This is my complete utility to do this kind of works with a ListView.
Moving items up, down, top, bottom, DragDrop move, etc all supporting Multiselect. basic export and import of items also possible.
Usage example:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ...  uListViewUtils;
  3. ...
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. begin
  6.   ListView1.OnDragDrop := @ListViewUtils.DragDrop;
  7.   ListView1.OnDragOver := @ListViewUtils.DragOver;
  8.   ListView1.OnMouseDown := @ListViewUtils.MouseDown;
  9.   ListView1.OnMouseMove := @ListViewUtils.MouseMove;
  10.   {$ifdef LINUX}
  11.     ListView1.DragMode := dmManual;
  12.   {$endif};
  13. end;
  14.  
  15. procedure TForm1.SpeedButton2Click(Sender: TObject);
  16. begin
  17.   ListViewUtils.DeleteItems(ListView1);
  18. end;
  19.  
  20. procedure TForm1.SpeedButton3Click(Sender: TObject);
  21. begin
  22.   ListViewUtils.MoveItemsUp(ListView1);
  23. end;
  24.  
  25. procedure TForm1.SpeedButton4Click(Sender: TObject);
  26. begin
  27.   ListViewUtils.MoveItemsDown(ListView1);
  28. end;
  29.  
  30. procedure TForm1.SpeedButton5Click(Sender: TObject);
  31. begin
  32.   ListViewUtils.MoveItemsTop(ListView1)
  33. end;
  34.  
  35. procedure TForm1.SpeedButton6Click(Sender: TObject);
  36. begin
  37.   ListViewUtils.MoveItemsBottom(ListView1);
  38. end;
  39.  
  40. procedure TForm1.SpeedButton7Click(Sender: TObject);
  41. begin
  42.   ListViewUtils.ToggleSelectAll(ListView1);
  43. end;
  44.  
  45. procedure TForm1.SpeedButton8Click(Sender: TObject);
  46. begin
  47.   ListViewUtils.ToggleCheckItems(ListView1);
  48. end;
  49.  

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: listview move item up or down
« Reply #4 on: July 23, 2017, 07:15:27 am »
Thanks to all my friends, Works perfect   :D :D
I have created two projects thanks to your help.

project one: (Simple but effective)

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  9.   Buttons;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     BitBtn1: TBitBtn;
  17.     BitBtn2: TBitBtn;
  18.     ListView1: TListView;
  19.     procedure BitBtn1Click(Sender: TObject);
  20.     procedure BitBtn2Click(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. var
  39.   enumerando: integer;
  40. begin
  41.     with Form1.ListView1.Items.Add do
  42.   begin
  43.   Caption:= '';
  44.   SubItems.Add('erick');
  45.   SubItems.Add('caceres');
  46.   SubItems.Add('peña');
  47.   end;
  48.  
  49.     with Form1.ListView1.Items.Add do
  50.   begin
  51.   Caption:= '';
  52.   SubItems.Add('juan');
  53.   SubItems.Add('galvez');
  54.   SubItems.Add('soto');
  55.   end;
  56.  
  57.       with Form1.ListView1.Items.Add do
  58.   begin
  59.   Caption:= '';
  60.   SubItems.Add('luis');
  61.   SubItems.Add('flores');
  62.   SubItems.Add('layme');
  63.   end;
  64.  
  65.     with Form1.ListView1.Items.Add do
  66.   begin
  67.   Caption:= '';
  68.   SubItems.Add('leo');
  69.   SubItems.Add('allca');
  70.   SubItems.Add('gonzales');
  71.   end;
  72.  
  73.        with Form1.ListView1.Items.Add do
  74.   begin
  75.   Caption:= '';
  76.   SubItems.Add('gustavo');
  77.   SubItems.Add('mares');
  78.   SubItems.Add('tacna');
  79.   end;
  80.  
  81.     with Form1.ListView1.Items.Add do
  82.   begin
  83.   Caption:= '';
  84.   SubItems.Add('fred');
  85.   SubItems.Add('tonasca');
  86.   SubItems.Add('sierra');
  87.   end;
  88.  
  89.      with Form1.ListView1.Items.Add do
  90.   begin
  91.   Caption:= '';
  92.   SubItems.Add('bernardo');
  93.   SubItems.Add('luna');
  94.   SubItems.Add('roca');
  95.   end;
  96.  
  97.         begin // volviendo a enumerar después de borrar
  98.         for enumerando := 0 to ListView1.Items.Count-1 do
  99.         ListView1.Items[enumerando].Caption := IntToStr(enumerando+1);
  100.         end;
  101. end;
  102.  
  103. procedure TForm1.BitBtn1Click(Sender: TObject);  // MOVE ITEM UP
  104. var
  105.   Sel: TListItem;
  106.   enumerando: integer;
  107. begin
  108.   ListView1.MultiSelect:=false;
  109.   Sel := ListView1.Selected;
  110.   if Assigned(Sel) then
  111.     if Sel.Index - 1 >= 0 then
  112.       ListView1.Items.Exchange(Sel.Index, Sel.Index - 1);
  113.  
  114.   ListView1.SetFocus;
  115.   ListView1.MultiSelect:=true;
  116.  
  117.         begin // volviendo a enumerar después de borrar
  118.         for enumerando := 0 to ListView1.Items.Count-1 do
  119.         ListView1.Items[enumerando].Caption := IntToStr(enumerando+1);
  120.         end;
  121. end;
  122.  
  123. procedure TForm1.BitBtn2Click(Sender: TObject);  // MOVE ITEM DOWN
  124. var
  125.   Sel: TListItem;
  126.   enumerando: integer;
  127. begin
  128.   ListView1.MultiSelect:=false;
  129.   Sel := ListView1.Selected;
  130.   if Assigned(Sel) then
  131.     if Sel.Index + 1 < ListView1.Items.Count then
  132.       ListView1.Items.Exchange(Sel.Index, Sel.Index + 1);
  133.  
  134.     ListView1.SetFocus;
  135.   ListView1.MultiSelect:=true;
  136.  
  137.         begin // volviendo a enumerar después de borrar
  138.         for enumerando := 0 to ListView1.Items.Count-1 do
  139.         ListView1.Items[enumerando].Caption := IntToStr(enumerando+1);
  140.         end;
  141. end;
  142. end.


Project two: (Excellent with advanced features)

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  9.   Buttons, uListViewUtils;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     BitBtn1: TBitBtn;
  17.     BitBtn2: TBitBtn;
  18.     BitBtn3: TBitBtn;
  19.     BitBtn4: TBitBtn;
  20.     BitBtn5: TBitBtn;
  21.     BitBtn6: TBitBtn;
  22.     BitBtn7: TBitBtn;
  23.     ListView1: TListView;
  24.     procedure BitBtn1Click(Sender: TObject);
  25.     procedure BitBtn2Click(Sender: TObject);
  26.     procedure BitBtn3Click(Sender: TObject);
  27.     procedure BitBtn4Click(Sender: TObject);
  28.     procedure BitBtn5Click(Sender: TObject);
  29.     procedure BitBtn6Click(Sender: TObject);
  30.     procedure BitBtn7Click(Sender: TObject);
  31.     procedure FormCreate(Sender: TObject);
  32.   private
  33.     { private declarations }
  34.   public
  35.     { public declarations }
  36.     procedure number_all;
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.   enumerando: integer;
  42.  
  43. implementation
  44.  
  45. {$R *.lfm}
  46.  
  47. { TForm1 }
  48.  
  49. procedure tform1.number_all;
  50. begin
  51.     // volviendo a enumerar después de borrar
  52.     for enumerando := 0 to ListView1.Items.Count-1 do
  53.     ListView1.Items[enumerando].Caption := IntToStr(enumerando+1);
  54. end;
  55.  
  56. procedure TForm1.FormCreate(Sender: TObject);
  57. var
  58.   enumerando: integer;
  59. begin
  60.     with Form1.ListView1.Items.Add do
  61.   begin
  62.   Caption:= '';
  63.   SubItems.Add('erick');
  64.   SubItems.Add('caceres');
  65.   SubItems.Add('peña');
  66.   end;
  67.  
  68.     with Form1.ListView1.Items.Add do
  69.   begin
  70.   Caption:= '';
  71.   SubItems.Add('juan');
  72.   SubItems.Add('galvez');
  73.   SubItems.Add('soto');
  74.   end;
  75.  
  76.       with Form1.ListView1.Items.Add do
  77.   begin
  78.   Caption:= '';
  79.   SubItems.Add('luis');
  80.   SubItems.Add('flores');
  81.   SubItems.Add('layme');
  82.   end;
  83.  
  84.     with Form1.ListView1.Items.Add do
  85.   begin
  86.   Caption:= '';
  87.   SubItems.Add('leo');
  88.   SubItems.Add('allca');
  89.   SubItems.Add('gonzales');
  90.   end;
  91.  
  92.        with Form1.ListView1.Items.Add do
  93.   begin
  94.   Caption:= '';
  95.   SubItems.Add('gustavo');
  96.   SubItems.Add('mares');
  97.   SubItems.Add('tacna');
  98.   end;
  99.  
  100.     with Form1.ListView1.Items.Add do
  101.   begin
  102.   Caption:= '';
  103.   SubItems.Add('fred');
  104.   SubItems.Add('tonasca');
  105.   SubItems.Add('sierra');
  106.   end;
  107.  
  108.      with Form1.ListView1.Items.Add do
  109.   begin
  110.   Caption:= '';
  111.   SubItems.Add('bernardo');
  112.   SubItems.Add('luna');
  113.   SubItems.Add('roca');
  114.   end;
  115.  
  116.         begin // volviendo a enumerar después de borrar
  117.         for enumerando := 0 to ListView1.Items.Count-1 do
  118.         ListView1.Items[enumerando].Caption := IntToStr(enumerando+1);
  119.         end;
  120.  
  121.   ListView1.OnDragDrop := @ListViewUtils.DragDrop;
  122.   ListView1.OnDragOver := @ListViewUtils.DragOver;
  123.   ListView1.OnMouseDown := @ListViewUtils.MouseDown;
  124.   ListView1.OnMouseMove := @ListViewUtils.MouseMove;
  125.   {$ifdef LINUX}
  126.     ListView1.DragMode := dmManual;
  127.   {$endif};
  128. end;
  129.  
  130. procedure TForm1.BitBtn1Click(Sender: TObject);  // MOVE ITEM UP
  131. begin
  132.      ListViewUtils.MoveItemsUp(ListView1);
  133.      number_all;
  134. end;
  135.  
  136. procedure TForm1.BitBtn2Click(Sender: TObject);  // MOVE ITEM DOWN
  137. begin
  138.       ListViewUtils.MoveItemsDown(ListView1);
  139.       number_all;
  140. end;
  141.  
  142. procedure TForm1.BitBtn3Click(Sender: TObject);
  143. begin
  144.   ListViewUtils.MoveItemsTop(ListView1);
  145.   number_all;
  146. end;
  147.  
  148. procedure TForm1.BitBtn4Click(Sender: TObject);
  149. begin
  150.   ListViewUtils.MoveItemsBottom(ListView1);
  151.   number_all;
  152. end;
  153.  
  154. procedure TForm1.BitBtn5Click(Sender: TObject);
  155. begin
  156.   ListViewUtils.DeleteItems(ListView1);
  157.   number_all;
  158. end;
  159.  
  160. procedure TForm1.BitBtn6Click(Sender: TObject);
  161. begin
  162.   ListViewUtils.ToggleSelectAll(ListView1);
  163. end;
  164.  
  165. procedure TForm1.BitBtn7Click(Sender: TObject);
  166. begin
  167.     ListViewUtils.ToggleCheckItems(ListView1);
  168. end;
  169.  
  170. end.



PD: Attach images and projects
Thanks again friends  :) :)

 

TinyPortal © 2005-2018