Lazarus

Programming => LCL => Topic started by: loaded on January 29, 2022, 07:34:31 am

Title: [Solved] Processing of selections in FileListBox content in order of selection?
Post by: loaded on January 29, 2022, 07:34:31 am
Hi All,
I have a code that gets selected files in FileListBox.
But if I want to get them in the order of selection, things get complicated.

As an alternaive ;
I thought about moving the rows up and down, but that didn't seem very practical to me.
Is there a ready solution for this on the FileListBox side? Or should I create my own solution?
Code: Pascal  [Select][+][-]
  1. var
  2.   i:integer;
  3.   p1:UTF8String;
  4. begin
  5.   for i:=0 to FileListBox1.Items.Count-1 do
  6.   begin
  7.     if FileListBox1.Selected[i] then
  8.     begin
  9.        p1:= '"'+FileListBox1.Directory+'\'+FileListBox1.Items[i]+'" ';
  10.        // Todo
  11.     end;
  12.   end;
  13. end;
I would be glad if you share your thoughts on the subject. Respects.
Title: Re: Processing of selections in FileListBox content in order of selection ?
Post by: Thaddy on January 29, 2022, 08:06:51 am
On select, add the selection to a queue and do not process yet. Then process the items in the queue. A queue is FIFO. There are several good (de)queues available in the standard fpc distribution. E.g. fgl, generics.collections,  contnrs. You can also use a Tstrings descendant as a queue.
I am not aware that a listbox (any) has such functionality, although re-ordering is possible.
I always would go for a queue, it is easy and exactly what you asked.
Title: Re: Processing of selections in FileListBox content in order of selection ?
Post by: loaded on January 29, 2022, 08:12:38 am
Thank you so much for the reply Thaddy
Yes, as you said, I can do this if the selection process is made one by one, but if multiple selections are made with the "shift" key from time to time, the circuits light up.  %)
Or I should have a coffee now, maybe I can do it later.
Title: Re: Processing of selections in FileListBox content in order of selection ?
Post by: ccrause on January 29, 2022, 08:23:29 am
Thank you so much for the reply Thaddy
Yes, as you said, I can do this if the selection process is made one by one, but if multiple selections are made with the "shift" key from time to time, the circuits light up.  %)
Or I should have a coffee now, maybe I can do it later.
While you wait for the coffee to kick in - if you want to really mimick the user actions, expand Thaddy's suggestion by tracking selection changes via OnSelectionChange event. If this event doesn't give you the required resolution, then hook all possible user actions (OnKeyDown/Up, OnMouseDown/Up etc.) and track the selection process that way (although this will require quite a bit of extra work).
Title: Re: Processing of selections in FileListBox content in order of selection ?
Post by: loaded on January 29, 2022, 08:33:50 am
Thank you so much for the reply ccrause
Yes, you are right, I drank my coffee and Lazarus I'm driving at full speed on the streets  of FileListBox behind the wheel of  :)
Title: Re: Processing of selections in FileListBox content in order of selection ?
Post by: engkin on January 29, 2022, 09:09:58 am
From user perspective, the order would not be clear. Find a better alternative like using two ListBoxes.
Title: Re: Processing of selections in FileListBox content in order of selection ?
Post by: loaded on January 29, 2022, 09:43:46 am
Thank you so much for the reply king
I specifically needed this feature for pdf merging with Ghostscript because pdfs need to be merged in the order they are selected.

Finally, a cup of coffee + 2 cups of linden + some inspiration and the result I want;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileListBox1SelectionChange(Sender: TObject; User: boolean);
  2. var
  3.   i,im:integer;
  4.   fsname:UTF8String;
  5.   mc:Boolean;
  6. begin
  7.   for im:=Memo_File_Selects.lines.count downto 0  do
  8.   begin
  9.     mc:=true;
  10.     for i:=0 to FileListBox1.Items.Count-1 do
  11.     begin
  12.       fsname:= '"'+FileListBox1.Directory+'\'+FileListBox1.Items[i]+'"';
  13.       if (FileListBox1.Selected[i]) and (fsname=Memo_File_Selects.Lines.Strings[im]) then
  14.       begin
  15.       mc:=false;
  16.       end;
  17.     end;
  18.     if mc then Memo_File_Selects.Lines.Delete(im);
  19.   end;
  20.  
  21.   for i:=0 to FileListBox1.Items.Count-1 do
  22.   begin
  23.     if FileListBox1.Selected[i] then
  24.     begin
  25.        fsname:= '"'+FileListBox1.Directory+'\'+FileListBox1.Items[i]+'"';
  26.        mc:=false;
  27.          for im:=0 to Memo_File_Selects.lines.count do
  28.          begin
  29.              if (fsname=Memo_File_Selects.Lines.Strings[im]) then
  30.              begin
  31.              mc:=true;
  32.              end;
  33.          end;
  34.          if not mc then  Memo_File_Selects.lines.Add(fsname);
  35.     end;
  36.   end;
  37. end;  


With the power of tea :)
I wrote a program on the subject and published it on my site, I also prepared a video and added it to youtube.
https://www.youtube.com/watch?v=_lNgJ1HuYHk
Title: Re: [Solved] Processing of selections in FileListBox content in order of selection?
Post by: engkin on January 29, 2022, 08:48:06 pm
Well done! It reminds me of PDFTools.
Title: Re: [Solved] Processing of selections in FileListBox content in order of selection?
Post by: loaded on January 30, 2022, 06:26:23 am
Well done! It reminds me of PDFTools.

Master engkin , I'm glad you like it. Whatever I did, I did it thanks to the masters in this forum. Thank you always be present.
TinyPortal © 2005-2018