Default
Google

How can I associate integer values with items?

Applies To: C++Builder 1 or higher
Category: Knowledge Base

Every component in C++Builder allows me to specify a Tag property. The compiler does not use this field but I can store in it my own information. Is it possible to associate an integer value with an item?
 
 1.
  Start a new project using File | New Application.
 
 2.
  Add a ListBox. Edit its OnClick event:
 
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
    if(ListBox1->ItemIndex != -1)
    {
        int tag = ListBox1->Perform(LB_GETITEMDATA,
            ListBox1->ItemIndex, 0);
        ShowMessage(tag);
    }
}
 
 3.
  Add a Button. Edit its OnClick event:
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int x;
 
    x = ListBox1->Items->Add("First");
    ListBox1->Perform(LB_SETITEMDATA, x, 55);
    x = ListBox1->Items->Add("Second");
    ListBox1->Perform(LB_SETITEMDATA, x, 56);
    x = ListBox1->Items->Add("Third");
    ListBox1->Perform(LB_SETITEMDATA, x, 57);
}
 
You can send an LB_SETITEMDATA message to set a 32-bit value associated with the specified item in a list box.

C++Builder Developer's Network
Copyright © Yoto Yotov



Acquiring image from ProHosting Banner Exchange