ListBox.SelectedIndexChanged - can you determine if it was user initiated?
Asked Answered
S

3

3

My question is similar to this: How to prevent ListBox.SelectedIndexChanged event?, but I want to ask it a different way.

Is there a simple way to determine if the 'SelectedIndexChanged' is coming from the user as opposed to initiated through code (e.g. ListBox.SelectedIndex = x)?

Steelworks answered 18/6, 2010 at 17:24 Comment(0)
B
4

As far as I know, no, there's no simple way built-in. The best I've been able to do is set a flag just before changing it in code and then letting the event handler reset the flag and return.

I suppose you could start examining the call stack and see if it's originating somewhere in your own code or not, but I'm not sure how much it's worth the effort.

Balladry answered 18/6, 2010 at 17:31 Comment(1)
That's what I thought, but I was holding out hope. Because, man, I hate jumping through the hoops you have to jump through (setting flags, etc). There needs to be something like click versus checkchanged (for radiobuttons). Unless someone tells us otherwise, you'll soon have a green check-mark! Thanks.Steelworks
C
4

For me, the 'SelectionChangeCommitted' event was better suited for my purposes. It fires when a selection in the drop down is selected. This is the easiest way to handle the specific case when the end-user initiates the change. SelectedIndexChanged is to capture all cases.

Cymric answered 19/7, 2010 at 19:46 Comment(1)
Excellent answer. Unfortunately, for me, Visual WebGUI does not implement that event (VWG is like WinForms, but sometimes not). And, the same issue would arise for ListBox's, or even the ComboBox on the ToolStrip. Nonetheless, still a good answer. Thanks for the info.Steelworks
S
2

Property change listeners don't distinguish between causes of a property change. It's a common problem. Setting a flag is the way to do it.

I do wish that there was a way to set values without firing property change events. But then, people argue that it breaks the whole object-oriented model, because it effectively allows you to change a field directly, without using a property.

Stretcher answered 18/6, 2010 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.