RAD Studio VCL Reference
|
Specifies the index of the selected item.
property ItemIndex: Integer;
__property int ItemIndex;
Read ItemIndex to determine which item is selected. The first item in the list has index 0, the second item has index 1, and so on. If no item is selected, the value of ItemIndex is -1. If the list control supports multiple selected items, ItemIndex is the index of the selected item that has focus.
Set ItemIndex programmatically to select an item by passing in the index value.
C++ Examples:
/* This is a very simple example showing how to create a comboBox, add to it some choices, selecting a default value and getting the selected value. */ void __fastcall TForm1::FormCreate(TObject *Sender) { //comboBox initialize comb = new TComboBox(this); comb->Parent = this; //visual options comb->Align = TAlign::alLeft; comb->DoubleBuffered = true; comb->AutoComplete = true; //adding items to the combo box comb->AddItem("firstChoice",NULL); comb->AddItem("secondChoice",NULL); comb->AddItem("thirdChoice",NULL); //setting the default value comb->ItemIndex = 1; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { MessageDlg("Selected text: " + comb->Text, mtInformation, TMsgDlgButtons() << mbOK, 0); }
/* This example uses a list box and a button on a form. The list box contains items when the form appears. When the user selects an item and clicks the button, the selected item in the list box is moved to the top of the list box: */ void __fastcall TForm1::FormCreate(TObject *Sender) { ListBox1->MultiSelect = false; Button1->Caption = "Move to Top"; for (int i = 1; i <= 10; i++) ListBox1->Items->Add("Item " + IntToStr(i)); } void __fastcall TForm1::Button1Click(TObject *Sender) { if (ListBox1->ItemIndex >= 0 && ListBox1->ItemIndex <= 9) ListBox1->Items->Move(ListBox1->ItemIndex, 0); }
Delphi Examples:
{ This is a very simple example showing how to create a comboBox, add to it some choices, selecting a default value and getting the selected value. } procedure TForm3.Button1Click(Sender: TObject); begin MessageDlg('Selected text: ' + comb.Text, mtInformation, mbYesNo, 0); end; procedure TForm3.FormCreate(Sender: TObject); begin //initialize the combo box comb := TComboBox.Create(Self); comb.Parent := Self; //visual options comb.Align := alLeft; comb.DoubleBuffered := true; comb.AutoComplete := true; //adding items to the combo box comb.AddItem('firstChoice', nil); comb.AddItem('secondChoice', nil); comb.AddItem('thirdChoice', nil); //setting the default value comb.ItemIndex := 1; end;
{ This example uses a list box and a button on a form. The list box contains items when the form appears. When the user clicks the button, the selected item in the list box is moved to the top of the list box: } procedure TForm1.Button1Click(Sender: TObject); begin if (ListBox1.ItemIndex in [0..9]) then ListBox1.Items.Move(ListBox1.ItemIndex, 0); end; procedure TForm1.FormCreate(Sender: TObject); var I: Integer; begin ListBox1.MultiSelect := False; Button1.Caption := 'Move to Top'; for I := 1 to 10 do ListBox1.Items.Add('Item ' + IntToStr(I)); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|