I have a project , I need to show the Names of Employes in a ListBox .

When a name is Selected , I get the Employee ID from the List box and use it to get the Employe data.

The ListBox does not have a place where you can store Values apart from the one Displayed.

In VB6 each ListBox Item has propertie called itemData.

This is a VB Example below

newIndex = Listbox1.Additem("Sandra Xeon")
ListBox1.itemData(newIndex) = 13456289 ' Sandra Employee ID

How can this be done in DELPHI.....I need Sample Code in Delphi

Thank you

Recommended Answers

All 2 Replies

You have to use the TList that stores an array of pointers.

Unit Classes

Description:TList, which stores an array of pointers, is often used to maintain lists of objects. TList introduces properties and methods to

Add or delete the objects in the list.
Rearrange the objects in the list.
Locate and access objects in the list.
Sort the objects in the list.


Lists the object references.

Delphi syntax:

property Items[Index: Integer]: Pointer; default;


Description

Use Items to obtain a pointer to a specific object in the array. The Index parameter indicates the index of the object, where 0 is the index of the first object, 1 is the index of the second object, and so on. Set Items to change the reference at a specific location.

Use Items with the Count property to iterate through all of the objects in the list.

Not all of the entries in the Items array need to contain references to objects. Some of the entries may be nil (Delphi) or NULL (C++) pointers. To remove the nil (Delphi) or NULL (C++) pointers and reduce the size of the Items array to the number of objects, call the Pack method.

Items is the default property for TList. This means you can omit the property name. Thus, instead of
MyList.Items
you can write
MyList


Inserts a new item at the end of the list.

Delphi syntax:

function Add(Item: Pointer): Integer;

C++ syntax:

int __fastcall Add(void * Item);

Description

Call Add to insert a new object at the end of the Items array. Add returns the index of the new item, where the first item in the list has an index of 0.

Add increments Count and, if necessary, allocates memory by increasing the value of Capacity.

Note: Add always inserts the Item pointer at the end of the Items array, even if the Items array contains nil (Delphi) or NULL (C++) pointers.

I have a project , I need to show the Names of Employes in a ListBox .

When a name is Selected , I get the Employee ID from the List box and use it to get the Employe data.

The ListBox does not have a place where you can store Values apart from the one Displayed.

In VB6 each ListBox Item has propertie called itemData.

This is a VB Example below

newIndex = Listbox1.Additem("Sandra Xeon")
ListBox1.itemData(newIndex) = 13456289 ' Sandra Employee ID

How can this be done in DELPHI.....I need Sample Code in Delphi

Thank you

Hi,

I tend to use
listbox1.items.addobject(myrec.name,pointer(i));
then retrieve the integer when selected from the listbox with
i := integer(listbox1.items.objects[listbox1.itemindex]);

so myrec.name is the employee name and i is the employee number

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.