Unexpected behavior of SelectAll method in TListBox lazarus / 31-LCL ---------------------------------------------------------------------- simone · 2020-04-17 21:32 I noticed unexpected (and unwanted) behavior in the TListBox component. When the number of items exceeds those visible in the control window, if the SelectAll method is invoked, during the selection there is an annoying downward scrolling of the items. How can this effect be prevented? Thanks in advance. ---------------------------------------------------------------------- winni · 2020-04-17 22:06 Hi! Rude code for rude components: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i idx := ListBox1.TopIndex;ListBox1.visible := false;ListBox1.SelectAll:ListBox1.visible := true;ListBox1.TopIndex := idx;  Perhaps you need a application.ProcessMessages after visible := false; Winni ---------------------------------------------------------------------- jamie · 2020-04-17 22:25 There must be a bug in the code then... With windows you should be able to do this.. SendMessage(ListBox.Handle, LB_SELITEMRANGE, 1, MakeLPARAM(0,-1)); That selects it all and it does not scroll the box over here. ---------------------------------------------------------------------- jamie · 2020-04-17 22:29 This one might be simpler as it selects only and does not give option for unselecting... but it will allow a larger list... SendMessage(ListBox.handle, LB_SELITEMRANGEEX, 0,-1); ---------------------------------------------------------------------- simone · 2020-04-17 22:31 @winni: Thanks Winni. Your solution, even if rude, works well. The problem is that when the items are many, the time required for their selection makes the temporary disappearance of the listbox perceptible to the user. However, this is certainly a big improvement. @Jamie: Now I try your solution. ---------------------------------------------------------------------- jamie · 2020-04-17 22:33 You will need to include the Windows unit; ---------------------------------------------------------------------- simone · 2020-04-17 22:47 Thanks Jamie. It's necessary to add also LCLIntf. Under Windows your solution works well, but I have used: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SendMessage(ListBox.handle, LB_SELITEMRANGEEX, 0,ListBox.Count-1); instead of Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SendMessage(ListBox.handle, LB_SELITEMRANGEEX, 0,-1); If I use this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SendMessage(ListBox.Handle, LB_SELITEMRANGE, 1, MakeLPARAM(0,-1));I get a range checking error during compilation ---------------------------------------------------------------------- winni · 2020-04-17 22:57 Hi! Now I got it again what I was looking for: The platform independent solution Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ListBox1.It ems.BeginUpdate;ListBox1.SelectAll;ListBox1.Items.EndUpdate;  Winni ---------------------------------------------------------------------- simone · 2020-04-17 23:13 This method works well! Thank you. ---------------------------------------------------------------------- jamie · 2020-04-17 23:18 Something must of changed, I don't get a range error when I do it.. Hmm, oh well.. ---------------------------------------------------------------------- jamie · 2020-04-17 23:21 Quote from: winni on April 18, 2020, 12:57:16 amHi! Now I got it again what I was looking for: The platform independent solution Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ListBox1.It ems.BeginUpdate;ListBox1.SelectAll;ListBox1.Items.EndUpdate;  That still scrolls down for me.. Try my solution, no changing of scroll.. Winni ---------------------------------------------------------------------- simone · 2020-04-17 23:27 @Jamie: It's my mistake: it's just a warning. I apologize... ---------------------------------------------------------------------- winni · 2020-04-17 23:37 Hi! Just tested how Simones problem behaves with Linux64/gtk2. No scrolling, no other problems. You both: Same old story: Change your OS! Winni ---------------------------------------------------------------------- jamie · 2020-04-18 00:02 I just looked at the code that does it, its not proper... it is not using the Widget to select all but cycling through the list one at a time which I guess could be ok but however, it leaves the last one that it processed and then scrolls it into view as the selected item..  It fills the Selected array which calls a Setter to do this of course an calls widget set code for each entry which in turn auto scrolls the selection in view, which now is the last one.  That is the normal behavior of a List box but the LCL is implementing it incorrectly for a SelectALL..  It should be restoring the previous selection so it does not scroll.  At least your Widget you are using should be doing the same to mimic the OS it is suppose to duplicate.. There is  fix for this however, I so think if the Loop counter was done backwards instead it would work out fine In all targets I suppose I could try that and recompile it.. ---------------------------------------------------------------------- winni · 2020-04-18 00:06 Hi! But after every selection of an item then "changed" is called, and that forces the visual update - the scrolling. Maybe you are lucky but I don't think so. Winni ---------------------------------------------------------------------- jamie · 2020-04-18 00:18 OK, well that is problem... However, I found a work around without changing the ListBox code... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i OldIndex := ListBox.ItemIndex;ListBox.SelectAll;ListBox.Selected[OldIndex]:= True;  that works.. or you can simply set the Selected[0] := True which is the top etc.. but in any case it is taking the last index that was set as the selected and scrolling into view.. this little hack seems to fix it without changing the LCL code. ---------------------------------------------------------------------- jamie · 2020-04-18 00:48 Ok, I got a combo of yours and mine that seems to work Log the TopIndex; Items.beginUpdate SelectAll; TopIndex := LoggedValue; items.EndUpdate; that seems to be stable and the scroll stays put... EDIT: BTW, this seems to be Delphi Compatible! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var  TI :Integer;Begin  TI := ListBox1.TopIndex;         {need to get the current top index that is showing}  Listbox1.Items.BeginUpdate;    {freeze any Widget to OS Updating }  ListBox1.SelectAll;                 {Older Delphi didn't have this, had to loop it ourselves  }  ListBox1.TopIndex := TI;         { Restore the TopIndex }  ListBox1.Items.EndUpdate;                {Now let the Widget update everything }  No Scroll ListBox SelectALL… ---------------------------------------------------------------------- simone · 2020-04-18 09:06 Thanks for hints. Since it seems to be a bug of LCL under Windows OS, I opened a new issue in bugtracker. ---------------------------------------------------------------------- jamie · 2020-04-18 11:31 Well actually the way its written overall in the LCL is incorrect for any target. The implementation for a SelectAll should be done at the widget level where It can simply call it's equal or implement it there for others that don't have it at the OS level.. ---------------------------------------------------------------------- Bart · 2020-06-07 15:06 See my remark in the bugtracker for a solution on LCL impelmentation level. The SendMessage solution requires we have a default WS implementation, so Win32 can override this. Also it won't work for > 65536 items in a ListBox, so we need a fallback for that as well. Bart ---------------------------------------------------------------------- ASerge · 2020-06-08 16:46 Quote from: Bart on June 07, 2020, 05:06:35 pmAlso it won't work for > 65536 items in a ListBox, so we need a fallback for that as well. Something like this (not tested): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i class procedure TWin32WSCustomListBox.SelectRange(const ACustomListBox: TCustomListBox;  ALow, AHigh: integer; ASelected: boolean); override;var  ListBoxWnd: HWND;begin  ListBoxWnd := ACustomListBox.Handle;  if AHigh <= High(Word) then // LB_SELITEMRANGE+MAKELPARAM limitation    if SendMessage(ListBoxWnd, LB_SELITEMRANGE, Ord(ASelected),      MAKELPARAM(ALow, AHigh) <> LB_ERR then    begin      Exit;    end;  SendMessage(ListBoxWnd, WM_SETREDRAW, 0, 0);  try    inherited;  finally    SendMessage(ListBoxWnd, WM_SETREDRAW, 1, 0);    RedrawWindow(ListBoxWnd, nil, 0, RDW_INVALIDATE or RDW_ALLCHILDREN or RDW_ERASE);  end;end; ---------------------------------------------------------------------- jamie · 2020-06-08 20:59 All the Redrawing code should not be needed. Windows already sends notices to the app after doing this.. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  SendMessage(ListBox1.Handle, LB_SELITEMRANGE,1,MakeLong(0,ListBox1.Count-1)); For example the above will select all of it and notified the app with proper messages.  There is one note however, if the user should pass a -1 for the ranges the code in the widget should adjust the values to the proper ranges..  For example if the High Value is -1 then it should change the value to the ListBox.Count-1;  Doing it this way the coder does not need to include the max list range of the listbox, it will just adjust it for them..  I don't think the windows API likes seeing a value past what is actually in there so this should only happen if the coder past a -1.. ---------------------------------------------------------------------- Bart · 2020-06-09 12:08 When I have the time I will try to implement this. It needs testers voor WinCE, once implemented. Further discussions in the bugtracker please. Bart ---------------------------------------------------------------------- ASerge · 2020-06-09 14:25 Quote from: jamie on June 08, 2020, 10:59:23 pmAll the Redrawing code should not be needed. Windows already sends notices to the app after doing this.. If you are referring to the code I have given, please note that redraw is called if the old method is used. ---------------------------------------------------------------------- Bart · 2020-06-10 10:06 I implemented this in r63336, r63337, rr63338, r63339. @All: thanks for pointing me to the solution. Please test. Can somebody please test the implementation in WinCE. In theory it should work, but I cannot test this myself. Bart ---------------------------------------------------------------------- ASerge · 2020-06-10 15:48 Quote from: Bart on June 10, 2020, 12:06:09 pmPlease test. This code works fine (of course, the first case works faster): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure FillListBoxWithSelect(ListBox: TListBox; const ItemsCount: SizeInt);var  i: SizeInt;begin  ListBox.Items.BeginUpdate;  try    ListBox.Clear;    for i := 0 to ItemsCount - 1 do      ListBox.Items.Append(IntToStr(i));  finally    ListBox.Items.EndUpdate;  end;  ListBox.ItemIndex := ItemsCount - 1;  ListBox.SelectAll;end; procedure TForm1.Button1Click(Sender: TObject);begin  FillListBoxWithSelect(ListBox1, 65000);end; procedure TForm1.Button2Click(Sender: TObject);begin  FillListBoxWithSelect(ListBox1, 66000);end; procedure TForm1.FormCreate(Sender: TObject);begin  ListBox1.MultiSelect := True;end;