Link to home
Start Free TrialLog in
Avatar of pavelmed
pavelmed

asked on

How to programmatically select a ComboBox item or value?

I have a programmatically populated ComboBox control (System.Windows.Forms) where each item contains text and value.
I populated the comtrol using statement like this:
cbxControl.Items.Add(new ListItem("text", "value"));

Now I need to programmatically select an item from the ComboBox control (in response of some user's action like a button click) and have the ComboBox to display the newly selected item (to change its previous selection).

Somehow this statement does not work:
cbxControl.SelectedIndex = index // an index that I want to select.

The question is: How to select an item from this ComboBox control and display the new selection?

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
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
Avatar of james920
james920

I also agree the code should work. Just a note that the ComboBox.SelectedIndex property is using zero-based index, so make sure the index value you are using is correct.
Avatar of pavelmed

ASKER

I have stepped through the code.  It goes through but does nothing - the selection corresponding to 0 index is displayed although I want to set it to a bigger index.  The box is populated.
Are you using Visual Studio as your IDE?  If yes, try to use the breakpoint (The function key F9) on your statement "cbxControl.SelectedIndex = index", and run your program through the function key F5.  If the program stops at the breakpoint then you can use the "QuickWatch" function to look at the variable "index" and see what the value is.  I suspect that you've either not execute the statement, or your variable index has mistakely set wrong.
It works on mine.
I have setup the index to an explicit number, like 5, 4, etc.  
So the actual statement was "cbxControl.SelectedIndex = 5; "
And the box contained many records.  Still it displayed the top record which was the empty string.

And I used the breakpoint to make sure the statement was executed as well.
Well, I have resolved this issue.  It appeared that the that ComboBox Control was not guilty.  It was set to a proper index, but later it was reset beck to 0, and I missed that because it was in a different place of the code.  So when I commented out that "reset", it worked OK.

Because my mistake caused you spend some time on this, I am splitting the points between developers who responded to this question.

I am not sure that this site allows me to split it between everybody, so I'll split between as many as possible starting with who responded first.
Thank you for your help.
I apologise for not splitting the points as I promissed.  I have not done this a while, and I missed a prompt to split the points becasue I thought it would be shown to me later.  As a result, points went to a person who responded first.
Anyway, I thank everybody who responded to this.
I know this thread ended a long time ago, but if you are using VB 6.0 you can set a ComboBox to a specific value by using the ListIndex property.  You just need to know what the ListIndex value is of the list item you want selected.  Below is a code snippet of how this might be done if you want to compare a text value given and match it up to its index.  This short program compares a value in a text box and then matches it the proper ListIndex of the item in the ComboBox when a checkbox is selected.
Private Sub chkCalc_Click(Index As Integer)
 
Dim i As Integer
 
If chkCalc.value = 1 Then
    
    If txtCalcType.text = "" Then
        i = 0
    End If
    
    If txtCalcType.text = "Average" Then
        i = 1
    End If
    
    If txtCalcType.text = "Maximum" Then
        i = 2
    End If
    
    If txtCalcType.text = "Minimum" Then
        i = 3
    End If
    
    If txtCalcType.text = "Value" Then
        i = 4
    End If
    
    cmboCalcType.ListIndex = i
End If
           
End Sub

Open in new window