combobox selectedindexchanged event

T

tbrown

I have a combobox with items like this: {one,two,three}. The selected
index is 0, so "one" appears in the combobox text. When the user drops
down the list, and selects "two", for example, I modify the Items
collection to be {two,one,three} and now want "two" to appear in the
combobox text. However, the combobox text is now blank. the is apparently
somehow the result of having changed the combobox.Items collection.

If, trying to fix this, I manually do this:

combobox1.SelectedIndex = 0;

This reinvokes the SelectedIndexChanged event and I am in an infinite
loop. The same thing happens if I change the text property of the
combobox instead of the selectedindex.

The behavior I am trying to accomplish is a drop down substitution list
for a basketball scoring program. The players in the game are listed in
combo boxes, and each combobox contains the set of players that could be
substituted for that player, so it dynamically changes whenever a
substitution is made.

Any ideas?

Terry Brown
Stickman Software
www.stickmansoftware.com
 
T

tbrown

I tried to fix this by setting the enabled property of the combobox to
false while modifying things, but that doesn't work either.

On Sat, 30 Dec
 
R

RobinS

Do you mean the first one in the list is always the one "playing", and
when you substitute someone else for them, you want the other person
to show up as the first one in the list instead?

Robin S.
 
T

tbrown

The list I build contains the player currently playing as the first entry
(this is so that the user can cancel the substitution by tapping
anywhere). That player is followed by a list of all other players on the
roster who are not playing (so they could enter the game), and this
portion of the list is sorted alphabetically/numerically.

The problem that arises is that everytime a substitution is made the
SelectedIndexChanged event is caused, and then, because a substitution was
made, the list changes (now the players not in the game are different),and
this change also causes a SelectedIndexChanged event.

I'm beginning to believe that .Net simply doesn't have a control that does
what I want. I have programmed PalmOS, and I want a popup list. In the
PalmOS control, changing the list items or order doesn't create an event,
the only event that's created is a selection from the list. If the user
pops up the list and then taps outside the boundaries of the list, the
list is dismissed with no event.

I can't seem to find or build a control that does this in .Net. Maybe
it's all way too complicated for my meager brain :)

Terry Brown
www.stickmansoftware.com
 
R

RobinS

Yeah, I'm getting to that. I'm first trying to figure out if my logic
was
the same as yours. So the first guy is the one playing, and
if they pick someone else in the list, they are now the one playing,
and everyone else is available as a substitute, so the one playing
goes to the top of the list. Right?

So you have an event handler for the SelectIndexChanged event
on the combobox, and when it fires, it reloads the combobox
(or changes the order or whatever). Right? And this is re-firing
the SelectIndexChanged event, which is causing your problem.
Is that right?

What you can do is define a boolean in your form called something
like "Loading". When loading your combobox, set the boolean to
True. Set the SelectedIndex to 0. When you are done loading the
combobox, set the boolean to False.

In your SelectIndexChanged event, before calling the Load routine,
check the boolean, and only call the load routine if the boolean
is False. This way, if the event is fired because of your load routine,
it will know not to fire it again because it's *you* mucking with the
SelectIndex and not the user.

Robin S.
------------------------------------
 
T

Tim Van Wassenhove

RobinS schreef:
What you can do is define a boolean in your form called something
like "Loading". When loading your combobox, set the boolean to
True. Set the SelectedIndex to 0. When you are done loading the
combobox, set the boolean to False.

A similar solution:

When the loading starts, remove the eventhandler...
And when the loading is done, attach it again...
 
T

Terry Brown

thanks for the suggestion, Robin. I'll try this with another combobox I
need to do. For now, I ripped out the combobox code and have implemented
the substitutions stuff with a dialog. I can dismiss the dialog with one
click by setting the DialogResult value. This works well, although I
don't consider it a good user interface choice.

I'll let you know how this works on the other piece of the design.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top