Recent

Author Topic: Scrollwheel position of Listbox  (Read 677 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Scrollwheel position of Listbox
« on: April 17, 2023, 03:20:17 pm »
Hello,

ist it possible to set and get the Position of the ListBox scrollbar ?

i need it because i use the Listbox just to display a List of Objects. Those Objects are provided by another class. and instead of add and remove i just clear the whole List and refill it (due to multiple changes in this List). This way the Listbox Scroll Position gets reset.

thanks in advance.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Scrollwheel position of Listbox
« Reply #1 on: April 17, 2023, 03:36:54 pm »
That should solve your problem.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   li: Integer;
  5. begin
  6.   li := ListBox1.TopIndex; // remember old position
  7.   ListBox1.Items.BeginUpdate;
  8.   ListBox1.Clear;
  9.   for i := 0 to 99 do
  10.     ListBox1.Items.Add('Test ' + IntToStr(i));
  11.   if (li <= Pred(ListBox1.Items.Count)) then
  12.     ListBox1.TopIndex := li; // set old position
  13.   ListBox1.Items.EndUpdate;
  14. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: Scrollwheel position of Listbox
« Reply #2 on: April 18, 2023, 01:14:55 pm »
That should solve your problem.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   li: Integer;
  5. begin
  6.   li := ListBox1.TopIndex; // remember old position
  7.   ListBox1.Items.BeginUpdate;
  8.   ListBox1.Clear;
  9.   for i := 0 to 99 do
  10.     ListBox1.Items.Add('Test ' + IntToStr(i));
  11.   if (li <= Pred(ListBox1.Items.Count)) then
  12.     ListBox1.TopIndex := li; // set old position
  13.   ListBox1.Items.EndUpdate;
  14. end;

Did the Trick ! thank you !

 

TinyPortal © 2005-2018