1
Reply

How do I set a combobox to a selected value when the combobox holds a class

Ray

Ray

Dec 24 2009 1:29 PM
5.5k

I'm stuck on this one...I have a combobox that i've filled as shown below (code thinned out for simplicity):
[code]
        // Load Material codes into combobox.
        intID = somenumber; //ex: 123, 234
        strItem = somestring; // ex: "white", "black"
        Material item = new Material(intID, strItem);
        cboMaterial.Items.Add(item);

// where Material is as shown below (code thinned out for simplicity):
        private class Material
        {
            private int intMaterialID;
            private string strMaterialCode;
            public Material(int intID, string strItem)
            {
                intMaterialID = intID;
                strMaterialCode = strItem;
            }
            public override string ToString()
            {
                return this.strMaterialCode;
            }
        }
[/code]
My question is that if later on in the code, if I know the value of "intID", how do I have the combobox display that selection?
While the following works, it's not what I need as I don't want to search for the strItem, but the intID.
[code]
cboMaterial.SelectedIndex = cboMaterial.FindStringExact("white")
[/code]


Answers (1)