Link to home
Start Free TrialLog in
Avatar of rockhammer99
rockhammer99

asked on

How do you set default selected in a combo box in c#.net

How do you set default selected in a combo box in c#.net
ASKER CERTIFIED SOLUTION
Avatar of Russell_Venable
Russell_Venable
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also take note you can do that this way as well.
cmbBox.SelectedIndex = cmbBox.FindString(@"YourValueHere");

Open in new window


This will give you the index of the Value you want. You can test it by setting a seperate integer and calling it like this.
int index = cmbBox.FindString(@"YourValueHere");
System.Windows.Forms.MessageBox.Show(index);

Open in new window

Try this method :
public void SearchComboBox(ComboBox cbTest,string searchValue,string searchFlag)
{
       switch(searchFlag)
       {
             case "I" :
                cbTest.SelectedIndex = Int32.Parse(searchValue);
                break;
             case "V" :
                cbTest.SelectedValue = searchValue;
                break;
             case "T" :
                cbTest.SelectedItem.ToString() = searchValue;
                break;
             default :
                cbTest.SetSelected(0,true);//Select First Index..     
                break;                  
       }
}

Open in new window

How to call above method ?? Answer is this way :
SearchComboBox(comboBox1,"2","I"); //TO SELECT 2nd Index Position.
SearchComboBox(comboBox1,"myValue","V"); //TO SELECT Value "myValue" from list..
SearchComboBox(comboBox1,"xyz","lkj"); //TO SELECT 0 index by default..

Open in new window

I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Hi,

I have provided the possible solution in comment http:#34922275 Pls review the answers for the question asked before taking decision. And accordingly close the issues.
I get these all the time even though I give them many different solutions. Seems to be more often too. Ignoring your own type of deal here. It's what we have come to know it as a "Fire & Forget" question.