Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Which is the best event to use when clicking / Filling a combo box
I have two combo boxes - both data bound.

I fill combo box 1 and then depending on what the user selects I fill combo box 2

When I fill combo box 1 the first item in the list is displayed, but I would prefer that not to display, so I set the selectedindex to -1 the problem with this is that it fires the selected index changed event twice.
Once with selectedindex = 0 then selectedindex = -1

So the problem is that the 2nd combox loads. (with the data for the selected index of 0)
The only way I can stop the 2nd combo box from loading is to flick a switch when filling the first combo box - Ie blnloading = true etc..

Is this the best way to do it or is there a better way or should I use a different event

Cheers
Posted

Dear Friend
In my view
While populating the combo box add one dummy item say "--select--" so that by default this option is selected and thus user can select the item
I think this will solve the issue
 
Share this answer
 
Comments
Manfred Rudolf Bihy 7-Jan-11 15:18pm    
A solution frequently used. 5+
Ants Hurdley 7-Jan-11 15:55pm    
good idea - thanks
thatraja 7-Jan-11 21:16pm    
Good one
There are a number of ways to get around this.

One way that I have done this in the past is to have a private bool variable on the form that I call something like pauseEvents.

Before you start to fill the ComboBox with values, you set pauseEvents to true. (or a more object-oriented way might be to have a function called PauseEvents that you call ie. this.PauseEvents() that sets the boolean value to true and then call another function called ResumeEvents.) Then, after you've finished filling it and setting the SelectedIndex to -1, set pauseEvents to true.

Then, in your SelectedIndexChanged event, the very first thing you have is an if statement...
C#
if(pauseEvents)
  return;


That will prevent anything else from running. IMO, there's nothing wrong with doing this.

You can also do as Vipin said. I generally do a combination of the two.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 7-Jan-11 15:17pm    
I don't exactly like that with the "bool on the form" part, but otherwise a viable solution. 5+
Ants Hurdley 7-Jan-11 15:56pm    
I figured this must be the only way - thanks very much

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