combobox find item by value
-
:(( Is there any way in a combobox to find the index of an item or set the item in a combobox by the value? I know there is a FindString, and a FindStringExact method in the combobox object, but is there anything simmilar or any work around to getting the index of an object or setting the selecteditem by the value? Any help is appreciated, thank you. Jason
-
:(( Is there any way in a combobox to find the index of an item or set the item in a combobox by the value? I know there is a FindString, and a FindStringExact method in the combobox object, but is there anything simmilar or any work around to getting the index of an object or setting the selecteditem by the value? Any help is appreciated, thank you. Jason
If I understand your request You want to find the index of a value in the combo box and also getting the selected item index ? well, for getting the index of a value in the combo box:
cmbBox.Items.IndexOf("Male");
will give you the index of the object with the value of "Male".. and to get the selected item index you can use the property "SelectedIndex":cmbBox.SelectedIndex
to get the index...(or to set it..) and:cmbBox.SelectedText
for a string orcmbBox.SelectedValue
for an object (get && set) next time, make a little search on our good friend Google... You could find it easly.. Good luck. -
If I understand your request You want to find the index of a value in the combo box and also getting the selected item index ? well, for getting the index of a value in the combo box:
cmbBox.Items.IndexOf("Male");
will give you the index of the object with the value of "Male".. and to get the selected item index you can use the property "SelectedIndex":cmbBox.SelectedIndex
to get the index...(or to set it..) and:cmbBox.SelectedText
for a string orcmbBox.SelectedValue
for an object (get && set) next time, make a little search on our good friend Google... You could find it easly.. Good luck.Without googl'ing it I already knew all of those properties you are telling me about (not to be arrogant, just to answer your reference to Google, I know better). You didn't understand the question, perhaps you haven't encountered this problem, if you have a combo box loaded with objects, say STATE objects, and the objects have 5-10 properties, and this is say on a customers form, when the user loads the info on a customer, you want to set the state the customer is from, and in your database, in the customers table you will only store the state id for the customer, and thus when you populate your customers form, you will only have the state id, unfortunately even if you create a state object with just the id set, when you do an indexof it won't match and give you nothing, and like I said, you have only the state id, which means no text, and if you do cmbBox.SelectedValue = myState.Id, it won't work even if you set the value member as ID... so what I'm after is something to this effect: myComboBox.SelectedIndex = [some function that returns the index of the object in combo box with ID = x] OR myComboBox.SelectedItem= [some function that returns the object in combo box with ID = x] OR myComboBox.SelectedValue = myState.Id Jason
-
Without googl'ing it I already knew all of those properties you are telling me about (not to be arrogant, just to answer your reference to Google, I know better). You didn't understand the question, perhaps you haven't encountered this problem, if you have a combo box loaded with objects, say STATE objects, and the objects have 5-10 properties, and this is say on a customers form, when the user loads the info on a customer, you want to set the state the customer is from, and in your database, in the customers table you will only store the state id for the customer, and thus when you populate your customers form, you will only have the state id, unfortunately even if you create a state object with just the id set, when you do an indexof it won't match and give you nothing, and like I said, you have only the state id, which means no text, and if you do cmbBox.SelectedValue = myState.Id, it won't work even if you set the value member as ID... so what I'm after is something to this effect: myComboBox.SelectedIndex = [some function that returns the index of the object in combo box with ID = x] OR myComboBox.SelectedItem= [some function that returns the object in combo box with ID = x] OR myComboBox.SelectedValue = myState.Id Jason
So in the beginning I didn’t understand your exact problem... Anyway, At the moment the best way I think about to solve this problem is creating your own function that runs on the list of states and search for the specific id... so the function will return the index.. to make it easier You can even inherit from the class Combo Box and overload the function indexOf so it will do the job.. When I will have a better answer for this problem I will notify you :-) Good luck!