none
Select by Value in ComboBox (Windows Forms)

    Question

  • Hello,

    This question relates to Windows Forms development in C# - not Web Development.

    I currently have an int value that is equal to the Value property of an item in a ComboBox control.  I want to be able to find the index of the ComboBox item with that value.  In pseudo C# I would do something like this:

    int myValue = 123;
    int theIndex = combobox1.getIndexOfValue(myInt);

    Then "theIndex" would be the index value I was looking for.  No such function exists however.

    Can anyone help me with how to do this?

    Thanks,

    Mark Young
    www.tip2tail.co.uk
    • Edited by tip2tail Monday, January 04, 2010 11:00 PM code formatting
    Monday, January 04, 2010 11:00 PM

Answers

  • Ok, i guess that could be caused by one of two things:
    1. the value you passed to your method does not actually exist in the combobox datasource.
    2. the ValueMember of the combobox is not set (I'd put my money on this one)

    if none of the above is not true please post the code where you initialize your combobox (set Datasource, ValueMember, DisplayMember) and the method in which you set SelectedValue.

    Best regards,
    Vladimir
    • Marked as answer by tip2tail Saturday, January 09, 2010 9:35 PM
    Thursday, January 07, 2010 1:30 AM

All replies


  • Hi, 
    can't you just iterate through the underlaying data source of the combobox and find the index of item with the specified value (123).
    You do know what the data source of the combo box is ? DataTable or something ...

    You could set the combobox's selected value and get the index but that would actually be visible on the form.

    comboBox1.SelectedValue = 123
    int index  = comboBox1.SelectedIndex


    It's a little unclear ... what are you gonna do with that index ?


    Best regards
    Vladimir
    • Marked as answer by Aland LiModerator Wednesday, January 06, 2010 11:01 AM
    • Unmarked as answer by tip2tail Wednesday, January 06, 2010 9:22 PM
    Tuesday, January 05, 2010 12:05 AM
  • Well the int value is being passed to the method as the value of what item in the combobox should be displayed.  Setting the selectedvalue doesn't change the combobox to show the selected item's text.  I have no way of knowing what the text that corresponds to the value will be.

    I need to be able to set the item whose value = the int I have to be displayed.

    Thanks,

    Mark
    Mark Young www.tip2tail.co.uk || mark@tip2tail.co.uk
    Wednesday, January 06, 2010 9:26 PM
  • Ok, i guess that could be caused by one of two things:
    1. the value you passed to your method does not actually exist in the combobox datasource.
    2. the ValueMember of the combobox is not set (I'd put my money on this one)

    if none of the above is not true please post the code where you initialize your combobox (set Datasource, ValueMember, DisplayMember) and the method in which you set SelectedValue.

    Best regards,
    Vladimir
    • Marked as answer by tip2tail Saturday, January 09, 2010 9:35 PM
    Thursday, January 07, 2010 1:30 AM
  • you can try like this
                int selectedIndex = comboBox1.Items.IndexOf(comboBox1.SelectedItem.ToString());
    
    • Marked as answer by tip2tail Saturday, January 09, 2010 9:35 PM
    • Unmarked as answer by tip2tail Saturday, January 09, 2010 9:35 PM
    Thursday, January 07, 2010 11:01 AM
  • Thanks,

    I managed to put something together with a for loop iterating through each value and then breaking at the right one!

    Mark
    Mark Young www.tip2tail.co.uk || mark@tip2tail.co.uk
    Saturday, January 09, 2010 9:36 PM
  • comboBox1.SelectedValue = 123
    int index  = comboBox1.SelectedIndex
    works great
    Thanks.
    Wednesday, October 06, 2010 12:32 AM