locked
How to select an item in combobox RRS feed

  • Question

  • I have a combo box with a list of book conditions. When a book is loaded into the form, the condition the book has needs to be selected. How can I do this? Here's what i have:

    Excellent
    Very Good
    Good
    Fair
    Poor

    just for example, how would i select "Good" WITHOUT USING THE INDEX? The database knows the condition text, and condition id. I need to select the condition in the combo box with either. However, I don't know how to add the condition id to the items in the combo box. How would I do this in Visual Basic 2008?

    **EDIT**

    I forgot to mention, although i am using a database, i am NOT using SQL Server. I just need a way to select it with a variable containing either the id or condition.
    Friday, October 9, 2009 9:05 PM

Answers

  • One of many ways:

    ComboBox1.SelectedIndex = ComboBox1.Items.IndexOf("Excellent")
    

    http://blogs.windowsclient.net/joes
    • Marked as answer by Jeff Shan Tuesday, October 13, 2009 7:08 AM
    Friday, October 9, 2009 9:13 PM
  • IndexOf returns the index of bound object not of display text I think. If you want to select the item based on its text, then cant you simply do?

     ComboBox1.Text = "Excellant"

    where item is selected based on its display value, if does not exist, combo remains with the previous value.





    Arjun Paudel
    • Proposed as answer by Waleed El-Badry Saturday, October 10, 2009 9:54 AM
    • Marked as answer by Jeff Shan Tuesday, October 13, 2009 7:08 AM
    Saturday, October 10, 2009 6:09 AM

All replies

  • One of many ways:

    ComboBox1.SelectedIndex = ComboBox1.Items.IndexOf("Excellent")
    

    http://blogs.windowsclient.net/joes
    • Marked as answer by Jeff Shan Tuesday, October 13, 2009 7:08 AM
    Friday, October 9, 2009 9:13 PM
  • I should have alse mentioned that if you have duplicates, this will select the first one. If you have a data source, it might be better to data bind the combo. It should look something like this, forgive me if this is off a bit, it's been awhile:

    Combobox1.DataSource = Table
    ComboBox1.DisplayMember = Condition
    ComboBox1.SelectedValue = ConditionId


    http://blogs.windowsclient.net/joes
    Friday, October 9, 2009 9:19 PM
  • Can this work with a MySql connection and reader? Thats why I said I needed to use it with a variable.
    Friday, October 9, 2009 9:21 PM
  • I'm sure I've tried this. I think it returned -1. I'll try again to be sure.
    Friday, October 9, 2009 9:22 PM
  • Nope, I tried this. It just returns -1.
    Friday, October 9, 2009 9:29 PM
  • Please post your code that you tried. Thanks.
    http://blogs.windowsclient.net/joes
    Friday, October 9, 2009 9:33 PM
  • Okay, I think i may have the data source right. its a dataview object correct? But how do i get the condition and condition id values?
    Friday, October 9, 2009 10:07 PM
  • Just the class that uses this, or ALL the classes used by the classes that use this too? It would be alot to post.
    Saturday, October 10, 2009 2:29 AM
  • Please post the code where the index is returning -1, along with any relevant info on the code or anything you may think would help aid in troubleshooting.


    http://blogs.windowsclient.net/joes
    Saturday, October 10, 2009 2:43 AM
  • IndexOf returns the index of bound object not of display text I think. If you want to select the item based on its text, then cant you simply do?

     ComboBox1.Text = "Excellant"

    where item is selected based on its display value, if does not exist, combo remains with the previous value.





    Arjun Paudel
    • Proposed as answer by Waleed El-Badry Saturday, October 10, 2009 9:54 AM
    • Marked as answer by Jeff Shan Tuesday, October 13, 2009 7:08 AM
    Saturday, October 10, 2009 6:09 AM
  • IndexOf would return the index of the string if the combo was not bound, and the type was string. If he passes the bound object, he would get the index. I think what is happening is that he is binding the object and then trying to get the IndexOf passing a string which will return -1. Again, this is an assumption until he can post more code.
    http://blogs.windowsclient.net/joes
    Saturday, October 10, 2009 7:29 AM