Recent

Author Topic: TListBox. First selected element and last selected element  (Read 866 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 422
TListBox. First selected element and last selected element
« on: October 30, 2025, 07:01:18 pm »
I think there should be methods that return first and last selected element in list box.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

ASerge

  • Hero Member
  • *****
  • Posts: 2477
Re: TListBox. First selected element and last selected element
« Reply #1 on: October 30, 2025, 11:55:14 pm »
I think there should be methods that return first and last selected element in list box.
Code: Pascal  [Select][+][-]
  1. {$IFDEF WINDOWS}
  2.   uses Windows, Messages;
  3. {$ENDIF}
  4.  
  5. {$IFDEF WINDOWS}
  6. procedure GetListSelInfo(AList: TListBox; out First, Last: Integer);
  7. var
  8.   Cnt: Integer;
  9.   Sels: array of Integer = nil;
  10. begin
  11.   First := -1;
  12.   Last := -1;
  13.   if AList = nil then
  14.     Exit;
  15.   Cnt := AList.Count;
  16.   SetLength(Sels, Cnt);
  17.   Cnt := SendMessage(AList.Handle, LB_GETSELITEMS, Cnt, LPARAM(Sels));
  18.   if Cnt > 0 then
  19.   begin
  20.     First := Sels[0];
  21.     Last := Sels[Cnt - 1]
  22.   end;
  23. end;
  24. {$ELSE}
  25. procedure GetListSelInfo(AList: TListBox; out First, Last: Integer);
  26. var
  27.   i: Integer;
  28. begin
  29.   First := -1;
  30.   Last := -1;
  31.   if AList = nil then
  32.     Exit;
  33.   for i := 0 to AList.Count - 1 do
  34.     if AList.Selected[i] then
  35.     begin
  36.       Last := i;
  37.       if First < 0 then
  38.         First := i;
  39.     end;
  40. end;
  41. {$ENDIF}

jamie

  • Hero Member
  • *****
  • Posts: 7607
Re: TListBox. First selected element and last selected element
« Reply #2 on: October 31, 2025, 12:58:29 am »
Code: Pascal  [Select][+][-]
  1.  Memo1.Text:= ListBox1.GetSelectedText;
  2.  

I find it strange you only want the first and last so maybe its a miss communication?

In any case, you can use the first and Last of the GetSelectedText results which can be sent to a TStringList.

Jamie

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7607
Re: TListBox. First selected element and last selected element
« Reply #3 on: October 31, 2025, 01:33:23 am »
You could always to a local Class hack and add this if you only need the start and end.

Code: Pascal  [Select][+][-]
  1. Function ListSelectedStartEnd(Var aStart,AEnd:Integer;Const AListBox:TListBox):Boolean;Inline;
  2. var
  3.   L:integer;
  4. Begin
  5.   AStart:=-1;AEnd:=-1;
  6.   With aListBox do
  7.   Begin
  8.   For L := 0 To Items.Count-1 do
  9.    Begin
  10.      If (aSTart <0)and(Selected[L])THen aSTart:=L;
  11.      If (aStart>=0)and(Selected[L])Then aEnd:=L;
  12.    end;
  13.   end;
  14.  Result := (aStart >=0);
  15. end;
  16.  
  17. procedure TForm1.Button1Click(Sender: TObject);
  18. Var
  19.   S,E:integer;
  20. begin
  21.  if ListSelectedStartEnd(S,E,ListBox1) Then
  22.  Caption := S.ToString+','+E.Tostring else
  23.  Caption := 'Nothing Selected';
  24. end;                                        
  25.  
  26.  
That should work in all platforms without change.
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 3307
Re: TListBox. First selected element and last selected element
« Reply #4 on: October 31, 2025, 08:04:26 am »
Jamie has it right: GetSelectedText is the easiest way

Aircode
Code: Pascal  [Select][+][-]
  1. Var sl:TStringList;
  2.  
  3. Begin
  4. .....
  5.   sl:=TStringList.Create;
  6.   sl.Text:=MyListBox.GetSelectedText;
  7.   Writeln('First=',sl.Strings[0],'-Last=',sl.Strings[sl.Count-1);
  8.  
  9. End;
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

LemonParty

  • Sr. Member
  • ****
  • Posts: 422
Re: TListBox. First selected element and last selected element
« Reply #5 on: October 31, 2025, 03:16:58 pm »
You could always to a local Class hack and add this if you only need the start and end.

Code: Pascal  [Select][+][-]
  1. Function ListSelectedStartEnd(Var aStart,AEnd:Integer;Const AListBox:TListBox):Boolean;Inline;
  2. var
  3.   L:integer;
  4. Begin
  5.   AStart:=-1;AEnd:=-1;
  6.   With aListBox do
  7.   Begin
  8.   For L := 0 To Items.Count-1 do
  9.    Begin
  10.      If (aSTart <0)and(Selected[L])THen aSTart:=L;
  11.      If (aStart>=0)and(Selected[L])Then aEnd:=L;
  12.    end;
  13.   end;
  14.  Result := (aStart >=0);
  15. end;
  16.  
  17. procedure TForm1.Button1Click(Sender: TObject);
  18. Var
  19.   S,E:integer;
  20. begin
  21.  if ListSelectedStartEnd(S,E,ListBox1) Then
  22.  Caption := S.ToString+','+E.Tostring else
  23.  Caption := 'Nothing Selected';
  24. end;                                        
  25.  
  26.  
That should work in all platforms without change.
I can write a loop that finds the first and the last one by myself, but it would be great if there is a method from the box. And this methods may find the n-th selected element from begin / end, that is better than just the last one and the first one.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Zvoni

  • Hero Member
  • *****
  • Posts: 3307
Re: TListBox. First selected element and last selected element
« Reply #6 on: October 31, 2025, 04:23:48 pm »
You could always to a local Class hack and add this if you only need the start and end.

Code: Pascal  [Select][+][-]
  1. Function ListSelectedStartEnd(Var aStart,AEnd:Integer;Const AListBox:TListBox):Boolean;Inline;
  2. var
  3.   L:integer;
  4. Begin
  5.   AStart:=-1;AEnd:=-1;
  6.   With aListBox do
  7.   Begin
  8.   For L := 0 To Items.Count-1 do
  9.    Begin
  10.      If (aSTart <0)and(Selected[L])THen aSTart:=L;
  11.      If (aStart>=0)and(Selected[L])Then aEnd:=L;
  12.    end;
  13.   end;
  14.  Result := (aStart >=0);
  15. end;
  16.  
  17. procedure TForm1.Button1Click(Sender: TObject);
  18. Var
  19.   S,E:integer;
  20. begin
  21.  if ListSelectedStartEnd(S,E,ListBox1) Then
  22.  Caption := S.ToString+','+E.Tostring else
  23.  Caption := 'Nothing Selected';
  24. end;                                        
  25.  
  26.  
That should work in all platforms without change.
I can write a loop that finds the first and the last one by myself, but it would be great if there is a method from the box. And this methods may find the n-th selected element from begin / end, that is better than just the last one and the first one.
GetSelectedText is a full string you can assign to a StringList.
Look there…..

And noone prevents you to write a classhelper
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

jamie

  • Hero Member
  • *****
  • Posts: 7607
Re: TListBox. First selected element and last selected element
« Reply #7 on: October 31, 2025, 10:48:41 pm »
One must remember that the Listbox can have random selections, they don't always have them listed contiguously.

This means if you are interested in the complete range of selections, there could be some in the middle not selected.

 But there is always that chance that maybe the object pointers of each entry is also of interest.


Jamie
The only true wisdom is knowing you know nothing

Aruna

  • Hero Member
  • *****
  • Posts: 794
Re: TListBox. First selected element and last selected element
« Reply #8 on: November 03, 2025, 03:45:46 am »
Hello Everyone,

A bit late to the discussion, but I’ve put together two sample projects that should help illustrate what everyone’s been talking about. There are multiple ways a user can click and select items in a TListBox.

The first example (shown in the first screenshot) demonstrates the simple case, tracking only the first and last selected elements.

As Jamie rightly pointed out, “One must remember that the ListBox can have random selections; they aren't always contiguous.” The second example (and its corresponding screenshot) handles exactly that scenario.

Hope this helps!

 

TinyPortal © 2005-2018