RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TComboBox Class

TComboBox combines an edit box with a scrollable list.

Pascal
TComboBox = class(TCustomComboBox);
C++
class TComboBox : public TCustomComboBox;

A TComboBox component is an edit box with a scrollable drop-down list attached to it. Users can select an item from the list or type directly into the edit box.

Note: The width of the button in a TComboBox is equal to the width that Windows uses for scrollbars. This width depends on the color scheme that the user has chosen (by right-clicking the desktop, selecting Properties, and choosing Appearance). On Windows XP, the user can specifically change the width of the scrollbar by clicking the Advanced button and selecting scrollbar as the Item. If you carefully size your combo box so all the text is visible, then a user running under a different color scheme may find that the text is obscured by the button. One solution is to set the scrollbar wide when developing the application. Most color schemes use a scrollbar that is at most 21 pixels wide.
 

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);
}

 

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;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!