Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I would like to programactically set a combobox back to the first item. Since the data in the combobox is dynamic, I can't just simply set the text to a value.

I've tried mycombobox.SelectedIndex = 0; - The code executes but it still displaying the previously selected item.

Any help would be greatly appreciated.

Thank you,

[UPDATE]
Hi,
I'm writing in WinForms and this event is attached at a SelectedIndexChanged for a different combination box. I thought it should work and I was doing it correctly, I'm just not sure why it's not. Maybe I need a call to Application.DoEvents(); ?
[/UPDATE]
Posted
Updated 7-Jan-11 5:30am
v3
Comments
Nuri Ismail 7-Jan-11 11:33am    
I've deleted your fake answer and updated your question with the information from your answer.
Next time if you want to clarify something about your question please just update your original question and/or post a comment to answer(s).

Are you writing WinForms/WPF/Web???
In any case. Make sure your code for .SelectedIndex = 0 is actually firing where you expect it to and that it isn't being overriden by another assignment somewhere else because the statement will work if it is used in the right place.

--Edit:
Based on the article Sandeep posted, you will need to do a .SelectedIndex = -1 followed immediately by a .SelectedIndex = 0 to guarantee that each and every time this block runs the result is the first item selected.
 
Share this answer
 
v2
Comments
Sandeep Mewara 7-Jan-11 11:31am    
Comment from OP:
Hi,

I'm writing in WinForms and this event is attached at a SelectedIndexChanged for a different combination box. I thought it should work and I was doing it correctly, I'm just not sure why it's not. Maybe I need a call to Application.DoEvents(); ?
fjdiewornncalwe 7-Jan-11 12:48pm    
You should never need to call Application.DoEvents(). The architecture of the form should be written so that you don't need to. (Personal opinion)
My suggestion would be to put a breakpoint on that line and see if it gets hit and set correctly. If it does get hit, examine all the objects carefully and you'll likely find the root of your problem.
Sandeep Mewara 7-Jan-11 13:07pm    
Article link I shared - it exactly points out the issue/bug that you pointed out about clearing.
Sandeep Mewara 8-Jan-11 12:49pm    
:) Ok.
5!
As much as I remmeber, it should be
mycombobox.SelectedIndex = -1;

Have a look at this article[^].
 
Share this answer
 
Comments
thatraja 7-Jan-11 11:37am    
You right, it's -1. 5!
fjdiewornncalwe 7-Jan-11 12:46pm    
Wrong... -1 removes all selected items. 0 is the index of the first item so you have to set to 0 to select the first item.
Sandeep Mewara 7-Jan-11 13:06pm    
Article link I shared - it exactly points out this issue/bug.
fjdiewornncalwe 8-Jan-11 12:47pm    
The article shows exactly why = -1 is incorrect. I the user wants the first item to get selected every time he executes this, then = 0 is the correct way to do it. The OP specifically isn't trying to clear the box, but select the first item. I think you just missed that little detail in his question. Your answer would be correct in all cases if you suggest that he select = -1 and follow that immediately with = 0. If he does that he can guarantee that the first item is selected. Cheers...
Member 10800885 6-May-17 2:30am    
Marcus... you are right :)
so, you initialise the value is -1.
 
Share this answer
 
Comments
fjdiewornncalwe 8-Jan-11 12:48pm    
Are you adding anything to a previous answer? Please read those before posting something like this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900