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!
pavelmedAsked:
Who is Participating?
 
strickddConnect With a Mentor Commented:
Have you stepped through your code to make sure that line is actually executing? The code you have should work.
0
 
james920Commented:
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.
0
 
pavelmedAuthor Commented:
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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
akumanovaCommented:
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.
0
 
yokkuiCommented:
It works on mine.
0
 
pavelmedAuthor Commented:
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.
0
 
pavelmedAuthor Commented:
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.
0
 
pavelmedAuthor Commented:
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.
0
 
Elisheva_BinyaminCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.