Hi All
I have a problem. I have a field that holds a country code and a combobox that has a list of of all countries. I would like to search for the country code from the combo box list of countries, get its index and display it in the combo box as the default item.
Any suggestions will be greatly appreciated.
Thanks

AlanRank #669 · 3.6K reputationPosted Nov 13, 2007, 1:23 PM EST
If the comboBox also contains country codes rather than country names, you need something like this:
// field declaration
string countryCode;
// in some method
countryCode = "fr"; // say
int index = comboBox1.FindString(countryCode);
comboBox1.SelectedIndex = index;
If the comboBox contains country names rather than country codes, then you could set up a Hashtable or (if you have .NET 2.0) a generic Dictionary to associate each country code with a country name, viz:
// field declaration dict = new Dictionary();
Dictionary
// in Form_Load or some other method:
dict.Add("fr", "France");
dict.Add("us", "United States");
// etc
// in some other method
countryCode = "fr"; // say
int index = comboBox1.FindString(dict[countryCode]);
comboBox1.SelectedIndex = index;
TC Power6 reputationPosted Nov 14, 2007, 6:59 AM EST
Thanks for the help. Below is the final code. I am using a collection which also has a dispaly object for code and description of country.
private
void pnlDriverAddress_Enter(object sender, EventArgs e){
if (Mode == eMode.Add || Mode == eMode.Edit){
if (cmbDACountry.SelectedIndex == -1){
int nIndex = MDI.CountryCollection.GetIndex(MDI.Country); if (nIndex != -1){
cmbDACountry.SelectedIndex = nIndex;
cmbDACountry.Text = cmbDACountry.SelectedItem.ToString();
}
}
}
}