[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to programmatically select a ComboBox item or value?

Posted on 2006-05-23
9
Medium Priority
?
1,180 Views
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!
0
Comment
Question by:pavelmed
9 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 1500 total points
ID: 16744094
Have you stepped through your code to make sure that line is actually executing? The code you have should work.
0
 

Expert Comment

by:james920
ID: 16745600
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
 

Author Comment

by:pavelmed
ID: 16746589
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:akumanova
ID: 16748502
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
 
LVL 3

Expert Comment

by:yokkui
ID: 16750331
It works on mine.
0
 

Author Comment

by:pavelmed
ID: 16751990
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
 

Author Comment

by:pavelmed
ID: 16752584
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
 

Author Comment

by:pavelmed
ID: 16752671
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
 

Expert Comment

by:Elisheva_Binyamin
ID: 24364834
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Screencast - Getting to Know the Pipeline

873 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