Bookstore

Recent

Author Topic: Handling of index in ListBox  (Read 801 times)

MaxCuriosus

  • Jr. Member
  • **
  • Posts: 66
Handling of index in ListBox
« on: February 18, 2020, 12:44:42 pm »
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]
  1. Procedure Display_List(SL:TStrings; Title:String; Index:Integer);
  2. Begin
  3.   Form1.ListBox1.Items.Clear;
  4.   Form1.ListBox1.Items.AddStrings(SL);
  5.   Form1.ListBox1.ItemIndex:=Index;
  6.   Form1.Caption:=Title;
  7.   WriteLn(Form1.ListBox1.ItemIndex);
  8. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 9784
Re: Handling of index in ListBox
« Reply #1 on: February 18, 2020, 12:53:50 pm »
Code: Pascal  [Select]
  1. Procedure Display_List(SL:TStrings; Title:String; aIndex:Integer);
  2. Begin
  3.   Form1.ListBox1.BeginUpdate;
  4.   Form1.ListBox1.Items.Clear;
  5.   Form1.ListBox1.Items.AddStrings(SL);
  6.   Form1.ListBox1.ItemIndex:=aIndex;
  7.   Form1.ListBox1.Endupdate;
  8.   Form1.Caption:=Title;
  9.   // optional, but leave it in for now
  10.   Application.ProcessMessages;
  11.   WriteLn(Form1.ListBox1.ItemIndex);
  12. 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.
« Last Edit: February 18, 2020, 12:58:50 pm by Thaddy »
I am more like donkey than shrek

MaxCuriosus

  • Jr. Member
  • **
  • Posts: 66
Re: Handling of index in ListBox
« Reply #2 on: February 18, 2020, 01:17:10 pm »
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

GetMem

  • Hero Member
  • *****
  • Posts: 3629
Re: Handling of index in ListBox
« Reply #3 on: February 18, 2020, 01:22:33 pm »
@MaxCuriosus
Code: Pascal  [Select]
  1.   ListBox1.Items.BeginUpdate;
  2.   try
  3.      //your code here
  4.   finally
  5.     ListBox1.Items.EndUpdate;
  6.   end;

MaxCuriosus

  • Jr. Member
  • **
  • Posts: 66
Re: Handling of index in ListBox
« Reply #4 on: February 18, 2020, 02:56:55 pm »
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

  • Hero Member
  • *****
  • Posts: 3356
Re: Handling of index in ListBox
« Reply #5 on: February 18, 2020, 06:05:28 pm »
The procedure
Code: Pascal  [Select]
  1. procedure Display_List(const aSL: TStrings; const aTitle: String; anIndex: Integer);
  2. begin
  3.   Form1.ListBox1.Items.BeginUpdate;
  4.   try
  5.     Form1.ListBox1.Items.AddStrings(aSL);
  6.     Form1.ListBox1.ItemIndex := anIndex;
  7.   finally
  8.     Form1.ListBox1.Items.EndUpdate;
  9.   end;
  10.   Form1.Caption := aTitle;
  11.   WriteLn(Form1.ListBox1.ItemIndex);
  12. 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

  • Hero Member
  • *****
  • Posts: 9784
Re: Handling of index in ListBox
« Reply #6 on: February 18, 2020, 06:12:48 pm »
I expected that - as explained- but it can be a platform issue.
I am more like donkey than shrek

wp

  • Hero Member
  • *****
  • Posts: 6830
Re: Handling of index in ListBox
« Reply #7 on: February 18, 2020, 06:20:03 pm »
MaxCuriosus: What is your operating system? Widgetset? Lazarus and FPC version?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

MaxCuriosus

  • Jr. Member
  • **
  • Posts: 66
Re: Handling of index in ListBox
« Reply #8 on: February 20, 2020, 12:21:42 am »
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

  • Hero Member
  • *****
  • Posts: 6830
Re: Handling of index in ListBox
« Reply #9 on: February 20, 2020, 12:42:37 am »
code 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.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jamie

  • Hero Member
  • *****
  • Posts: 2460
Re: Handling of index in ListBox
« Reply #10 on: February 20, 2020, 03:26:02 am »
More than likely a message delay not setting the values..


After setting the index try calling the Application.processMessages so that it gets updated..
Number 1 at blue screen app creations!

MaxCuriosus

  • Jr. Member
  • **
  • Posts: 66
Re: Handling of index in ListBox
« Reply #11 on: February 20, 2020, 01:06:36 pm »
jamie,
I tried what you suggested, but no dice.

MaxCuriosus

  • Jr. Member
  • **
  • Posts: 66
Re: Handling of index in ListBox
« Reply #12 on: February 20, 2020, 01:13:59 pm »
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

  • Hero Member
  • *****
  • Posts: 3356
Re: Handling of index in ListBox
« Reply #13 on: February 20, 2020, 01:54:21 pm »
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

  • Jr. Member
  • **
  • Posts: 66
Re: Handling of index in ListBox
« Reply #14 on: February 21, 2020, 12:02:22 am »
howardpc,
I've rebuild this little project from scratch, but the same abnormal behavior persist.
Attached is a zip file of the project folder.