* * *

Author Topic: TListBox.Items.Add not having last added item to be selected (Noobie Question)  (Read 335 times)

BrainChemistry

  • Jr. Member
  • **
  • Posts: 53
Hi there,

when adding new items to TListBox by TListBox.Items.Add(S: String) the created item is selected automatically, hence triggers TListBoxSelectionChange. I'd like to have it rather to be like for a TComboBox. There a new item is added just "silently" and NOT selected on adding. 

Thanks for your help in advance.
BC

howardpc

  • Hero Member
  • *****
  • Posts: 2684
On which platform are you trying this?
Adding items programmatically to a listbox on Linux does not select the item (nor trigger OnSelectionChange).

wp

  • Hero Member
  • *****
  • Posts: 4602
Are you sure? Changing the selection when items are added to the listbox is not default behavior. Please run attached demo: A click on the button adds an item to the listbox, and when the selection of the listbox changes a line is added to the memo. But this does not happen.

I tested on Windows and Linux (gtk2 and qt).
« Last Edit: June 04, 2018, 07:20:35 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Handoko

  • Hero Member
  • *****
  • Posts: 2299
  • My goal: build my own game engine using Lazarus
I tested wp's code on Lazarus 1.8.0 64-bit Gtk2 Linux. The selection didn't change and the OnSelectionChange wasn't triggered.

@BrainChemistry

Please provide more info (OS, Lazarus version, widgetset, etc) and your sample code. Or at least you should try wp's code.

BrainChemistry

  • Jr. Member
  • **
  • Posts: 53
Thanks everybody, and esp. for the code sample!

I'm confused. Okay, I did manage to detect what I did wrong, although I did it on purpose because it shouldn't lead to this behaviour.

Well, as you all stated, it is not the TListBox.Items.Add(S: String) that triggers TListBoxSelectionChange, but pls check the next line of my code:

Code: Pascal  [Select]
  1. TexListBox.Items.Add(S: String);
  2. TexListBox.ItemIndex := i;
  3.  

So what actually triggers TListBoxSelectionChange, is the allocation of i (counting var in a for-loop) to the item index (read/write property). How is this triggering TListBoxSelectionChange? - To be clear, as soon as I comment out the second line, everything works as expected. Anyway, in my case I need to make sure the item indices are set as shown. Any explanations and/or ideas?

Just to share the infos as requested: Win8.1-32bit, Laz 1.6.4, FPC 3.0.0.

EDIT: See attached file.
« Last Edit: June 04, 2018, 08:12:57 pm by BrainChemistry »

wp

  • Hero Member
  • *****
  • Posts: 4602
Code: Pascal  [Select]
  1. TexListBox.Items.Add(S: String);
  2. TexListBox.ItemIndex := i;
  3.  
So what actually triggers TListBoxSelectionChange, is the allocation of i (counting var in a for-loop) to the item index (read/write property). How is this triggering TListBoxSelectionChange?
The event OnSelectionChange fires when - well - the selected item of the listbox changes to another item. The property of the listbox which identifies the index of the selected item is called "ItemIndex".  Properties usually can be read and written. If you code "i := Listbox.ItemIndex" then you read the index of the selected item and store it in variable i. Conversely if you do "Listbox.ItemIndex := i" you set the ItemIndex to be the valie of i -- this effectively *selects*  the item with the specified index. And this generates the event OnSelectionChange if a different item was selected before. And since, as you describe, i is the counter of a "for" loop, ItemIndex changes again and again.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

BrainChemistry

  • Jr. Member
  • **
  • Posts: 53
Code: Pascal  [Select]
  1. TexListBox.Items.Add(S: String);
  2. TexListBox.ItemIndex := i;
  3.  
So what actually triggers TListBoxSelectionChange, is the allocation of i (counting var in a for-loop) to the item index (read/write property). How is this triggering TListBoxSelectionChange?
The event OnSelectionChange fires when - well - the selected item of the listbox changes to another item. The property of the listbox which identifies the index of the selected item is called "ItemIndex".  Properties usually can be read and written. If you code "i := Listbox.ItemIndex" then you read the index of the selected item and store it in variable i. Conversely if you do "Listbox.ItemIndex := i" you set the ItemIndex to be the valie of i -- this effectively *selects*  the item with the specified index. And this generates the event OnSelectionChange if a different item was selected before. And since, as you describe, i is the counter of a "for" loop, ItemIndex changes again and again.

Thanks for the elaboration on that, it helped me see now, that my understanding of ItemIndex has been different from what it actually is.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus