Selecting popupmenu initial value means double change event firing

I hope I explained this simple problem accurately.
I have a popupmenu with several values and the initial value is one that I can select. The change event is the only event here and the one where actions happen. However The change event is somehow fired twice.
If I try to set the listindex to -1 I don’t get a second fire, but I no longer have an initial value.
What do I do?

Subclass popupmenu, add a private boolean property “UserAction”. Add Event Definitions “Change” and MouseDown".

In the MouseDown event, add

UserAction=True Return MouseDown

In the Change event, add

if UserAction then UserAction=False Change end

In the MouseExit event, add

#if TargetMacOS then UserAction=False #endif

The last bit is needed (according to my ancient notes) because Windows always fires MouseExit after MouseDown.

Now the subclassed popupmenu will only fire its Change event if the user actually clicked on it.

Thank you. It didn’t happen again because of your suggestions. I didn’t subclass the Popup, but I did add a property, and also added the Mousedown.
The change did not keep firing.
One other interesting thing that happened was I had a breakPoint on the change event and it would refire as long as that breakPoint was there.
This didn’t happen after changes were made. I am not sure whether adding the Mousedown or the property was the factor, but it worked totally

Glad it worked for you. The advantage of making a subclass is that you can then use your new custom popup class whenever you want this functionality. I added a couple of other features that suit my needs and now use my own class instead of the “stock” popup control in all my projects.

The various PopupMenu, ComboBox, and Listbox controls will send a change event whenever the selection changes:
When the user clicks on an item (obvious), or your program sets/changes the item (not obvious).

You’re getting the ‘changed’ event when you preset the selection in your program.

I will often create a Boolean Property Initialized, default False in a window that may have several of these controls.
Then in the controls Change event:

If Initialized then // respond to change EndIf

When I need to fill and set or reset the controls

Iniitalized = False // fill in the controls & preset Initialized = True

I take a similar approach to John (with a mbLoading property), and just want to add that you can remove a nested level of code if you return early:

[code]Sub Change() Handles Change
if mbLoading = true then return

// Handle actual change updates here
End Sub
[/code]

I find it a little bit easier to see than nested code (especially when you start adding if statements within that nest).

:slight_smile:

Or you can set the control enabled to false while it’s loading and do the same thing without an extra variable floating around. The drawback to this approach is that you might have to do this for dozens of controls whereas the mbLoading property is set/reset once. This approach works well if you’ve got that one control that likes to fire when you don’t want it to

[code]Sub Change() Handles Change
if me.enabled = false then return

// Handle actual change updates here
End Sub[/code]

I believe the new Framework controls won’t set off the events when set via code. In iOS projects it works this way and I’m assuming that whatever is coming will be similar. It solves some problems but it will be a pain to upgrade to.

[quote=380620:@Tim Parnell]
I find it a little bit easier to see than nested code (especially when you start adding if statements within that nest).
:)[/quote]
That’s actually how I’ve been coding more recent projects, I shouldn’t post from memory so late.

[quote=380649:@Bob Keeney]
I believe the new Framework controls won’t set off the events when set via code. In iOS projects it works this way and I’m assuming that whatever is coming will be similar. It solves some problems but it will be a pain to upgrade to.[/quote]
I hope not, I use the ‘changed via code’ event quite a bit.

That’s the way it is in iOS. I reported it as a bug and it was closed immediately ‘as designed’. I don’t know if the plan is to bring that same philosophy to the next framework or not. Perhaps I can ask at XDC in a couple of weeks.