Handling of index in ListBox lazarus / 31-LCL ---------------------------------------------------------------------- MaxCuriosus · 2020-02-18 11:44 With the code below the initial focus/index doesn't match the value of the Index variable, but is always set according to the default property value. However reading back the property value shows the correspondence with the Index variable value. What am I missing? 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 Display_List(SL:TStrings; Title:String; Index:Integer);Begin  Form1.ListBox1.Items.Clear;  Form1.ListBox1.Items.AddStrings(SL);  Form1.ListBox1.ItemIndex:=Index;  Form1.Caption:=Title;  WriteLn(Form1.ListBox1.ItemIndex);end; ---------------------------------------------------------------------- Thaddy · 2020-02-18 11:53 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 Display_List(SL:TStrings; Title:String; aIndex:Integer);Begin  Form1.ListBox1.BeginUpdate;  Form1.ListBox1.Items.Clear;  Form1.ListBox1.Items.AddStrings(SL);  Form1.ListBox1.ItemIndex:=aIndex;  Form1.ListBox1.Endupdate;  Form1.Caption:=Title;  // optional, but leave it in for now  Application.ProcessMessages;  WriteLn(Form1.ListBox1.ItemIndex);end;Note it is not a good idea to call your index index: too many places where that is the case and it is also a Pascal modifier. Also note that clear and addstrings can be costly, that's why you want to use begin/endupdate. ---------------------------------------------------------------------- MaxCuriosus · 2020-02-18 12:17 Form1.ListBox1.BeginUpdate; Form1.ListBox1.Endupdate; gives me these errors: identifier idents no Member "BeginUpdate" identifier idents no Member "EndUpdate" with Lazarus 2.0.2 and FPC 3.0.4 ---------------------------------------------------------------------- balazsszekely · 2020-02-18 12:22 @MaxCuriosus 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.Items.BeginUpdate;  try     //your code here  finally    ListBox1.Items.EndUpdate;  end; ---------------------------------------------------------------------- MaxCuriosus · 2020-02-18 13:56 GetMem, with your suggestion I don't get the compiler errors, nor the desired behavior. The setting of the index doesn't change anything w/r/t my initial post. I am still perplexed. ---------------------------------------------------------------------- howardpc · 2020-02-18 17:05 The procedure 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 Display_List(const aSL: TStrings; const aTitle: String; anIndex: Integer);begin  Form1.ListBox1.Items.BeginUpdate;  try    Form1.ListBox1.Items.AddStrings(aSL);    Form1.ListBox1.ItemIndex := anIndex;  finally    Form1.ListBox1.Items.EndUpdate;  end;  Form1.Caption := aTitle;  WriteLn(Form1.ListBox1.ItemIndex);end;works as intended here. What change are you expecting that setting a new index value fails to achieve for you? Obviously if index is larger than aStrings.Count-1 you will get an exception, and a negative index value removes the listbox selection. ---------------------------------------------------------------------- Thaddy · 2020-02-18 17:12 I expected that - as explained- but it can be a platform issue. ---------------------------------------------------------------------- wp · 2020-02-18 17:20 MaxCuriosus: What is your operating system? Widgetset? Lazarus and FPC version? ---------------------------------------------------------------------- MaxCuriosus · 2020-02-19 23:21 howardpc, I haven't try yet what you suggested, but I will. wp, code run on this configuration: Lazarus 2.02, FPC 3.04, Debian 9 ---------------------------------------------------------------------- wp · 2020-02-19 23:42 Quote from: MaxCuriosus on February 20, 2020, 12:21:42 amcode run on this configuration: Lazarus 2.02, FPC 3.04, Debian 9 I checked on Lubuntu and on Manjaro, gtk2/qt/qt5, Laz Trunk, FPC 3.04 -> no problem. Since your Laz version is not recent I guess that the problem has been fixed in the meantime. ---------------------------------------------------------------------- jamie · 2020-02-20 02:26 More than likely a message delay not setting the values.. After setting the index try calling the Application.processMessages so that it gets updated.. ---------------------------------------------------------------------- MaxCuriosus · 2020-02-20 12:06 jamie, I tried what you suggested, but no dice. ---------------------------------------------------------------------- MaxCuriosus · 2020-02-20 12:13 howardpc, 1) I am well aware of the effects of index out-of-bound and -1 value, but that's not the issue. 2) I've tried your code (after adding the missing "Form1.ListBox1.Items.Clear;") but without success. 3) Here is a little bit of context: I'm trying to handle a small set of lists/menus in a conceptually very common hierarchical tree-like structure, by controlling a set of external indexes/identifiers and using just one Form and one ListBox to display the content of one list/menu at a time. It is the sole form, created once and always visible. This implies that I need to control the ListBox entry index for "this level" list/menu upon return from a "sub-level" list/menu. I'm using one event handler, OnKeyDown in the ListBox, to parse 3 virtuals keys, VK_ESCAPE to terminate the application; VK_PRIOR to determine the parameters to be fed to the Display_List procedure in order to return to the "upper level" list/menu; VK_RETURN to display a "sub-level" list/menu determined by the currently indexed string as an identifier, and fed to the Display_List procedure (with an entry index of zero). I hope that this clarifies what I want to achieve with a minimalist code. ---------------------------------------------------------------------- howardpc · 2020-02-20 12:54 I think you must show a compilable example that demonstrates your problem, since several simple attempts by others, based only on your code snippet, not a complete example, fails to reproduce what you see. ---------------------------------------------------------------------- MaxCuriosus · 2020-02-20 23:02 howardpc, I've rebuild this little project from scratch, but the same abnormal behavior persist. Attached is a zip file of the project folder. ---------------------------------------------------------------------- winni · 2020-02-20 23:16 Hi! Seems you are missing a good Operating System. Just tested your code with Linux: No errors, no problems, subitems shown,  back to mainmenue works. Winni ---------------------------------------------------------------------- howardpc · 2020-02-21 00:08 Like winni, I can't see any problems with your program, except the lack of a Form1 OnDestroy handler to free your dispSL stringlist. ---------------------------------------------------------------------- MaxCuriosus · 2020-02-21 01:11 Quote from: winni on February 21, 2020, 12:16:57 amHi! Seems you are missing a good Operating System. Just tested your code with Linux: No errors, no problems, subitems shown,  back to mainmenue works. Winni 1) What do you mean by a good Operating System? Debian 9 is not a good O.S? 2) The program has been compiled on a Debian 9 system with Lazarus 2.02 and FPC 3.0.4  and run on the same computer, and another computer with Debian 9, and with Tails 4.3. Same anomaly each time. 3) Yes I can go to the sub-menu and come back, but it always return to the first line, regardless of the prior selection in the main menu. ---------------------------------------------------------------------- howardpc · 2020-02-21 08:51 I simplified your app and rewrote it according to what I think you want, as a single unit. Does this in fact behave as you expect on your OS? ---------------------------------------------------------------------- MaxCuriosus · 2020-02-21 17:40 howardpc, First thank you for simplifying and streamlining my code. The flow logic for navigating the forms doesn't match the desired behavior, however, I've noticed the zeroing of the variable "Key" when parsing the virtual keys in the event handler, and that's the solution to the problem that has been nagging me all along. Now even my initial program works on any platform/configuration. For good measure I've attached a revised version of your code. Thank you very much for the time you spent on this issue. P.S. I suspect that a similar issue could arise when using StringGrids.