Recent

Author Topic: Simulate a click on the first item of a TFileListBox  (Read 1573 times)

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Simulate a click on the first item of a TFileListBox
« on: March 11, 2021, 08:30:05 am »
Hello!

I have made a little application for viewing text files of a folder. There is a TFileListBox. When the user clicks on an item (or changes the selected item with keyboard arrows), the file is loaded into a TMemo.

I would like that, when the directory changes, the first file of the list to be loaded, as if the user had clicked on the first item. It sounds easy, but I don't find the way to do that.

The application is here: https://github.com/rchastain/textview

Any other suggestion on this project would be also welcome.

Regards.

Roland
My projects are on Gitlab and on Codeberg.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Simulate a click on the first item of a TFileListBox
« Reply #1 on: March 11, 2021, 08:56:47 am »
Since TFileListBox is descended from TCustomListBox, my money would be either on Property ItemIndex or Property Selected

https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcustomlistbox.selected.html
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Re: Simulate a click on the first item of a TFileListBox
« Reply #2 on: March 11, 2021, 09:26:40 am »
Since TFileListBox is descended from TCustomListBox, my money would be either on Property ItemIndex or Property Selected

Thank you for your answer. Indeed, this seems to work:

Code: Pascal  [Select][+][-]
  1.   FileListBox1.ItemIndex := 0;
  2.   FileListBox1.Click;
My projects are on Gitlab and on Codeberg.

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Re: Simulate a click on the first item of a TFileListBox
« Reply #3 on: March 11, 2021, 09:52:24 am »
Here is what I did:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadFirstItem;
  2. begin
  3.   if FileListBox1.Items.Count > 0 then
  4.   begin
  5.     FileListBox1.ItemIndex := 0;
  6.     FileListBox1.Click;
  7.   end else
  8.     Memo1.Clear;
  9. end;

Not sure that the organisation of my code be optimal, but it seems to work.
My projects are on Gitlab and on Codeberg.

 

TinyPortal © 2005-2018