Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Listbox with value for text item
Wed, Jul 15 2015 12:06 PMPermanent Link

Trinione

Hi:
How can a value be associated with the ListBox item?

In Pascal I can do the following:

 ListBox1.AddItem('Item 1', TObject(111));
 ShowMessage(IntToStr(Integer(ListBox1.Items.Objects[ListBox1.Items.IndexOf('Item 1')])));

This doesn't work in EWB2.
Wed, Jul 15 2015 12:59 PMPermanent Link

Uli Becker

That's not possible, at least for now.
You can create a second stringlist that holds the desired values with matching indexes.

Uli
Thu, Jul 16 2015 1:40 PMPermanent Link

Trinione

Thank you.
Thu, Jul 16 2015 6:21 PMPermanent Link

Steve Gill

Avatar

Hi Trinione,

I use a TStringList to get around this.  When I add the values to the Listbox, I also add them to the StringList.

ListBox1.AddItem('Item 1');

StringList1.Values['Item 1'] := IntToStr(111);

Then when the value is selected in the ListBox, I read the corresponding value from the StringList.

ShowMessage(StringList1.Values[ListBox1.Items[ListBox1.ItemIndex]]);

Hope that helps.

= Steve
Thu, Jul 16 2015 6:44 PMPermanent Link

Trinione

<< Steve Gill wrote:

Hi Trinione,
I use a TStringList to get around this.  When I add the values to the Listbox, I also add them to the StringList.
ListBox1.AddItem('Item 1');
StringList1.Values['Item 1'] := IntToStr(111);
Then when the value is selected in the ListBox, I read the corresponding value from the StringList.
ShowMessage(StringList1.Values[ListBox1.Items[ListBox1.ItemIndex]]);

Hope that helps.>>

Steve:
Thanks. That does help and is great reference if and when I use this method in the future. Hopefully the EWB would have the ability to store a code/value along with the text at some point, or the AddObject() method as is the standard way to do it in Delphi.

What I decided to go with though is using an un-bound Grid with the first column Visibile := False and Width := 0.

       With Grid1 do
       begin
         AppendRow;
         intCurrentRow := RowCount - 1;
         Rows[intCurrentRow][0] := sCode;
         Rows[intCurrentRow][1] := sMemberName;
       end;

The selected row data is retrieved via:

 sSelectedCode := Grid1.Rows[Grid1.RowIndex][0];

Much simpler to manage.


Fri, Jul 17 2015 6:43 PMPermanent Link

Steve Gill

Avatar

Hi Trinione,

<< What I decided to go with though is using an un-bound Grid with the first column Visibile := False and Width := 0.>>

That's what I do as well when I am using a TGrid although I generally use the last column, not that it makes any difference.

= Steve
Image