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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting Text in a Combo Box 1

Status
Not open for further replies.

Lukeage

Programmer
Mar 11, 2004
5
GB
Hi all,

I'm not really a great programmer so I'm hoping somebody out there can help me out. I have a Combo Box on a form which has pre-defined items. What I need to do is programmatically change the item shown in the combo box to another in the list when a button is pressed. Added to this burdon, I'm also trying not only to select a different item in the list, but also to highlight it as if it had been selected by mouse click.

The hair I still have left is getting greyer by the day.

Hope someone can help me out.

 
hstijnen,

Thanks for responding. I can't do that, the problem is that the item in the list that I need to pick is defined by an item read from a database (which is text). So if the item read from the database is 'Invoice' then I need to switch focus to the 'Invoice' item, if it is 'Letter' then I need to switch to the 'Letter' item (the index offset will be unknown because I'm using only text). I guess what I'm going to have to consider doing is using a switch case statement to identify which Index offset applies given the item read from the database.....then I can use ItemIndex as you have suggested.

Thanks for the help, much appreciated
 
howdy,

maybe you should just iterate through the list to see which item is in which place:

Code:
AnsiString StringFromDatabase;

for(int a = 0; a < ComboBox->Items->Count; a++)
{
 if(ComboBox->Items[a] == StringFromDatabase)
 {
  ComboBox->ItemIndex = a;
 }
}

NOTE:
You may have to replace the
Code:
Items[a]
with this
Code:
Items->operator[](a)
. They are supposed to be the same thing programmatically, but I think i have had trouble getting the previous to compile before....

Good Luck,
onrdbandit
 
Look at the IndexOf function:

Code:
  ComboBox->ItemIndex = ComboBox->Items->IndexOf(StringFromDatabase);

Good Luck.
 
Thanks everybody for your help. I've solved the problem using the replies that you gave me. Special thanks to itsgsd for the best one liner I've ever copied that works.

This place is fantastic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top