Recent

Author Topic: [SOLVED] TCheckListBox: Scroll first selected item to top  (Read 476 times)

Hansaplast

  • Hero Member
  • *****
  • Posts: 783
  • Tweaking4All.com
    • Tweaking4All
[SOLVED] TCheckListBox: Scroll first selected item to top
« on: April 24, 2026, 02:10:03 pm »
I'm using a TCheckListBox with quite a few items in it (200+).
When at least one item has been checked, I'd like the list to scroll that item to the top (only at the form.onShow event - not while checking items).

No matter what I try, I seem to be unable to accomplish this.

What I have tried:

Code: Pascal  [Select][+][-]
  1. TCheckListBox.TopIndex := X;
This only works when the checked item is not yet visible.

Code: Pascal  [Select][+][-]
  1. TCheckListBox.ItemIndex := X;
  2. TCheckListBox.MakeCurrentVisible;
This only scrolls the selected item into the visible area, as expeced, which often results the checked row to appear at the bottom of the visible area.

Code: Pascal  [Select][+][-]
  1. TCheckListBox.ScrollBy(0, Y); // Y being an arbitrary number
This doesn't do anything at all, Y being positive or negative ... nothing happens.

Code: Pascal  [Select][+][-]
  1. TCheckListBox.ScrollBy)WS(0, Y); // Y being an arbitrary number
I'm not sure what the purpose of this is, but the error "TWinControl.ScrollBy_WS: Handle not allocated" indicates that I cannot use this with Cocoa [see: documentation]).

I have no access to the vertical scrollbar with TChecListBox.

Does anyone know how this can be accomplished?
Or is this a bug as I would assume setting "TopIndex" would do what I'm looking for (documentation says: Index of the first visible (or top-most) item)?


For reference:
Lazarus 4.99 (rev main_4_99-3421-g449b0a887c) FPC 3.3.1 x86_64-darwin-cocoa, macOS Tahoe 26.4.1 using the latest Commandline Tools.
« Last Edit: April 28, 2026, 01:49:22 pm by Hansaplast »

Hansaplast

  • Hero Member
  • *****
  • Posts: 783
  • Tweaking4All.com
    • Tweaking4All
Re: TCheckListBox: Scroll first selected item to top
« Reply #1 on: April 28, 2026, 01:49:05 pm »
Hacky work around:
Most certainly not the cleanest way and it feels kind-a sketchy way to do it this way, but for now for Cocoa this seems to work:

Checklistbox.TopIndex := Checklistbox.Count-1;

Code: Pascal  [Select][+][-]
  1. for counter:=0 to Checklistbox.Count-1 do
  2.   if Checklistbox.Checked[Counter] then
  3.     begin
  4.       Checklistbox.TopIndex:=Counter;
  5.       break;
  6.     end;

And this only works with an onPaint event of one of the other controls on the form (or parent TPanel).
This does not work in an onShow event somehow.

Just posting it here in case someone is running into the same issue.

rich2014

  • New Member
  • *
  • Posts: 38
Re: [SOLVED] TCheckListBox: Scroll first selected item to top
« Reply #2 on: April 28, 2026, 04:36:30 pm »
it should be the issue in TCheckListBox.TopIndex.

commit a bug report please.

Hansaplast

  • Hero Member
  • *****
  • Posts: 783
  • Tweaking4All.com
    • Tweaking4All
Re: [SOLVED] TCheckListBox: Scroll first selected item to top
« Reply #3 on: April 28, 2026, 04:48:56 pm »
Thanks Rich014, will do 😊

Hansaplast

  • Hero Member
  • *****
  • Posts: 783
  • Tweaking4All.com
    • Tweaking4All
Re: [SOLVED] TCheckListBox: Scroll first selected item to top
« Reply #4 on: April 28, 2026, 05:02:36 pm »
Bugtracker: 42259

mas steindorff

  • Hero Member
  • *****
  • Posts: 589
Re: [SOLVED] TCheckListBox: Scroll first selected item to top
« Reply #5 on: April 28, 2026, 07:37:11 pm »
I understand this is marked solved but I have a suggestion.
I too had a long list of checkboxes (around 60) to work with but went to a tStringGrid after trying to enhance the checkboxes during run time. the Grid comes with more group operators.
see https://wiki.freepascal.org/TStringGrid
"CheckBox Column" section
MAS
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Hansaplast

  • Hero Member
  • *****
  • Posts: 783
  • Tweaking4All.com
    • Tweaking4All
Re: [SOLVED] TCheckListBox: Scroll first selected item to top
« Reply #6 on: April 28, 2026, 07:56:31 pm »
Oh cool, interesting approach - thank you for sharing 😊
I will look into this one as a backup solution for sure.

 

TinyPortal © 2005-2018