Recent

Author Topic: ListBox Position  (Read 538 times)

pentilisea

  • New Member
  • *
  • Posts: 34
ListBox Position
« on: February 27, 2023, 10:53:46 am »
Hi Guys,
back again with a stupid question.

Even though I googled it, I'm not getting a positive result.

I need tp pass the last Item of a ListBox (a number, not nessesarily an Integer) into a Variable or better into a Edit.Box or Label.

I managed to select last Item, but failed to pass it further. First item also would be an option. I only managed to pass the number of items..

Please look at my code here and correct it. Sorry for the inconveniance .....

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


procedure TForm1.Button1Click(Sender: TObject);

  var iTest: Integer;
      gesamt: Integer;

begin

  ListBox1.Clear;


  OpenDialog1.Filter:='txt-Dateien|*.txt';
  if OpenDialog1.Execute then
  ListBox1.Items.LoadFromFile(OpenDialog1.Filename);


  for i:=ListBox1.Items.Count - 1 downto 0 do
        if not TryStrToInt(ListBox1.Items, iTest) then
        ListBox1.Items.Delete(i);

  ListBox1.ItemIndex := ListBox1.Items.Count-1;

  ListBox1.ClearSelection;
  ListBox1.Selected[ListBox1.Count-1] := TRUE;



    gesamt:= ListBox1.ItemIndex;

    Edit1.Text := ListBox1.ItemIndex.ToString;

   Label1.Caption := OpenDialog1.Filename;

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Thanks

Klaus

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1405
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ListBox Position
« Reply #1 on: February 27, 2023, 11:29:15 am »
Code: Pascal  [Select][+][-]
  1.   Label1.Caption := ListBox1.Items.Strings[Pred(ListBox1.Count)];
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1405
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ListBox Position
« Reply #2 on: February 27, 2023, 11:34:18 am »
Please put your code into [ code ] mycode [ /code ] tags by pressing [ # ] button, thanks.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

pentilisea

  • New Member
  • *
  • Posts: 34
Re: ListBox Position
« Reply #3 on: February 28, 2023, 08:05:17 pm »
Hi Zwerg,
thanks so much. I will try to adhere to the code thing next time.

What stands "Pred" for?

Klaus

paweld

  • Hero Member
  • *****
  • Posts: 684
Re: ListBox Position
« Reply #4 on: February 28, 2023, 08:32:58 pm »
-1   
Code: Pascal  [Select][+][-]
  1. Label1.Caption := ListBox1.Items.Strings[ListBox1.Count - 1];
Best regards / Pozdrawiam
paweld

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1405
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ListBox Position
« Reply #5 on: February 28, 2023, 08:50:00 pm »
Hi Zwerg,
thanks so much. I will try to adhere to the code thing next time.

What stands "Pred" for?

Klaus
In math like paweld wrote, in words Pred() means Predecessor. (-1)
The opposite would be Succ() for Successor. (+1)
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

cdbc

  • Hero Member
  • *****
  • Posts: 500
    • http://www.cdbc.dk
Re: ListBox Position
« Reply #6 on: March 02, 2023, 08:03:01 pm »
Hi
TListbox has a Caption property...
Code: Pascal  [Select][+][-]
  1. ...
  2. ListBox1.ItemIndex := ListBox1.Items.Count-1;
  3. ...
  4. Label1.Caption := ListBox1.Caption; // when you set itemindex you get its text in caption
  5. ...
  6.  
Hth - Regards Benny
If it ain't broke, don't fix it ;)

 

TinyPortal © 2005-2018