Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probably an easy TComboBox question

Status
Not open for further replies.

Vovin

Programmer
Aug 24, 2003
63
GB
Hi,

this is probably really easy but I'm a C++ programer and I'm still trying to find my way around Delphi.

I have a combo box and an edit box on a form. If the user adds text to the edit box and clicks a button on my form I want the text to be added to the combo box (this I can do), and I also want the new text item to be highlighted/selected within the combo box.

How do I select an item in the combo box programmatically? I know what the text that I just added is, can I search for that?

Here is a wee bit of my code:
(where EditOther is the edit box)
if EditOther.Text <> '' then begin
ReasonsComboBox.Items.Add(EditOther.Text);
// TODO: highlight (select) the new reason in the combo box.

EditOther.Text := '';
end;

 
Something like this should work:
Code:
 if EditOther.Text <> '' then begin
  ReasonsComboBox.Items.Add(EditOther.Text);
  ReasonsComboBox.Items.ItemIndex := ReasonsComboBox.Items.Count - 1;
  EditOther.Text := '';
 end;
I'm assuming that you are allowing duplicate items to be added to you combo box. If you ignore duplicates then you need to use the IndexOf function to obtain the index of the required item which you can then assign to ItemIndex.


Andrew
Hampshire, UK
 
Thanks for your help towerbase, that did the trick.

Although the line should be:
ReasonsComboBox.ItemIndex := ReasonsComboBox.Items.Count - 1;
and not
ReasonsComboBox.Items.ItemIndex := ReasonsComboBox.Items.Count - 1;

Thanks.
 
That's what happens if I don't test the code first!

Glad to be of help.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top