Recent

Author Topic: First Item in ListBox by index/ID (solved)  (Read 436 times)

pentilisea

  • New Member
  • *
  • Posts: 34
First Item in ListBox by index/ID (solved)
« on: March 24, 2023, 02:57:08 pm »
The easy bit sometimes gets aekward.

With Gambas it's:
ListBox7[0].Text - and you have a string of the first Item of the listbox.

Tried a lot, but didn't find something working on my lazarus project.

Thanks for helping out without yawning.... but it gets a bit more wiered.

The basic idea is to transfer all items from ListBox1 to ListBox6.

Clear ListBox1 and then transfer all items of listbox6 back to
ListBox1 - but! in reverse order.

So result should be that previous first Item of ListBox1 is now
last item in ListBox1.

Trying all afternoon and if I had an Uzzi, I'd shoot the computer
if that helped....

So a txt file with 150 numbers, 1 number each line, is nicely loaded
into ListBox1, as needed for a lot of operations which all are working
OK.

Code: Pascal  [Select][+][-]
  1. OpenDialog1.Filter:='txt-Dateien|*.txt';
  2.   if OpenDialog1.Execute then
  3.   ListBox1.Items.LoadFromFile(OpenDialog1.Filename);
  4.  
  5.   for i:=ListBox1.Items.Count - 1 downto 0 do begin
  6.     ListBox1.Items[i] := Trim(ListBox1.Items[i]);
  7.     if not TryStrToInt(ListBox1.Items[i], iTest) then
  8.       ListBox1.Items.Delete(i);
  9.       end; //its designed to eliminate unwanted text
  10.  

The "First To Last" button has the following code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button96Click(Sender: TObject);
  2.  
  3.  var
  4. i: Integer;
  5.  
  6. begin
  7.  
  8.   for i := ListBox1.Count -1 downto 0 do begin;
  9.   ListBox1.ItemIndex := ListBox1.Items.Count-1;
  10.  
  11.   ListBox6.Items.Add((ListBox1.Items.Strings[Pred(ListBox1.Count -1)]));
  12.   ListBox1.Items.Delete(ListBox1.ItemIndex);
  13.   end;
  14.   ListBox6.ItemIndex := ListBox6.Items.Count-1;
  15.   ListBox1.ItemIndex := ListBox1.Items.Count-1;
  16.   ListBox1.Clear;
  17.  
  18. end;                  
  19.  

Although only haf of the task -
That does not work and LazTracer comes with some complaint.
If I set the downto to 1, instead of 0, hi, all goes well.
(I allways check that all the end; statements are wehre they should be...)

As said before, now all items of LixtBox 6 need to get back into
ListBox1 in revers order....

Maybe the operation can be done with using ListBox1 only.

« Last Edit: March 25, 2023, 01:41:30 pm by pentilisea »

paweld

  • Hero Member
  • *****
  • Posts: 683
Re: First Item in ListBox by index/ID
« Reply #1 on: March 24, 2023, 03:08:29 pm »
Code: Pascal  [Select][+][-]
  1. ShowMessage(ListBox7.Items[0]);  
  2. //or    
  3. //ShowMessage(ListBox7.Items.Strings[0]);
Best regards / Pozdrawiam
paweld

pentilisea

  • New Member
  • *
  • Posts: 34
Re: First Item in ListBox by index/ID
« Reply #2 on: March 24, 2023, 06:25:54 pm »
Code: Pascal  [Select][+][-]
  1. ShowMessage(ListBox7.Items[0]);  
  2. //or    
  3. //ShowMessage(ListBox7.Items.Strings[0]);

Thanks Pawl, works ok, ols see the extended post...

paweld

  • Hero Member
  • *****
  • Posts: 683
Re: First Item in ListBox by index/ID
« Reply #3 on: March 24, 2023, 08:40:01 pm »
Please do not edit the content of the post after answering, because the answer looks like out of context. If you have other questions then post them in new posts. 
As for your question about reordering, it can only be done with ListBox1 
Code: Pascal  [Select][+][-]
  1. var
  2.   i: Integer;  
  3. begin  
  4.   ListBox1.Items.BeginUpdate;  
  5.   i := ListBox1.Count - 1;  
  6.   while i > 0 do  
  7.   begin  
  8.     ListBox1.Items.Move(0, i);  
  9.     Dec(i);  
  10.   end;  
  11.   ListBox1.Items.EndUpdate;  
  12. end;
  13.  
Best regards / Pozdrawiam
paweld

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1404
  • Fifty shades of code.
    • Delphi & FreePascal
Re: First Item in ListBox by index/ID
« Reply #4 on: March 24, 2023, 09:54:01 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button96Click(Sender: TObject);
  2.  
  3.  var
  4. i: Integer;
  5.  
  6. begin
  7.  
  8.   for i := ListBox1.Count -1 downto 0 do begin; /// <<<<<<<<<< look how your line ends and when you use "i", why not use it at all?
  9.   ListBox1.ItemIndex := ListBox1.Items.Count-1;
  10.  
  11.   ListBox6.Items.Add((ListBox1.Items.Strings[Pred(ListBox1.Count -1)]));
  12.   ListBox1.Items.Delete(ListBox1.ItemIndex);
  13.   end;
  14.   ListBox6.ItemIndex := ListBox6.Items.Count-1;
  15.   ListBox1.ItemIndex := ListBox1.Items.Count-1;
  16.   ListBox1.Clear;
  17. end;                  
(I allways check that all the end; statements are wehre they should be...)
You should also check at "begin" what you doing.
Code: Pascal  [Select][+][-]
  1.   if (ListBox1.Count > 0) then
  2.     for i := Pred(ListBox1.Count) downto 0 do
  3.       begin
  4.         ListBox6.Items.Add((ListBox1.Items.Strings[i]));
  5.         ListBox1.Items.Delete(i);
  6.       end;
Just another way of possibility
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

pentilisea

  • New Member
  • *
  • Posts: 34
Re: First Item in ListBox by index/ID
« Reply #5 on: March 25, 2023, 01:41:11 pm »
Hi guys,
thanks Mr. zwerg and Paweld, all your answers work perfectly.

I wasted an afternoon poking araund.

Special thanks to paweld who solved my problem with s couple of lines using only 1 ListBox. Genial!!!

I need to finish my project, but my brain isn't made for thi stuff...

 

TinyPortal © 2005-2018