How to programmatically select a ComboBox item or value?

Posted on 2006-05-23
Last Modified: 2012-06-27
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!
Question by:pavelmed
    LVL 28

    Accepted Solution

    Have you stepped through your code to make sure that line is actually executing? The code you have should work.

    Expert Comment

    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.

    Author Comment

    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.
    LVL 2

    Expert Comment

    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.
    LVL 3

    Expert Comment

    It works on mine.

    Author Comment

    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.

    Author Comment

    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.

    Author Comment

    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.

    Expert Comment

    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


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    This article will show, step by step, how to integrate R code into a R Sweave document
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now