Default
Google

How can I detect selections?

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

 
 1.
  Start a new project using File | New Application.
 
 2.
  Add a ListBox. Insert some sample items.
 
 3.
  Add a Button. Edit its OnClick event:
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString sel;
    if(ListBox1->MultiSelect)
    {
        for(int x = 0; x < ListBox1->Items->Count; x++)
        if(ListBox1->Selected[x])
            sel += ListBox1->Items->Strings[x] + " ";
    }
    else
        sel = ListBox1->Items->Strings[ListBox1->ItemIndex];
    ShowMessage(sel);
}
 
If MultiSelect is set to true, the user has the right to select multiple items. This means that your code has to check each item individually. If, however, MultiSelect is false, the ItemIndex value will contain the index of the currently selected item.
 
Tips
· If ItemIndex returns -1, no item in the ListBox is selected.

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



Acquiring image from ProHosting Banner Exchange