How to remove/ deactivate an event?

youki 886 Reputation points

I have to disable a SelectionChanged event so it doesn't loop.
I enable it like so:

lvMouseKeys().SelectionChanged({ this, &MainWindow::lvMouseKeys_SelectionChanged });

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
470 questions
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,753 questions
No comments
{count} votes

Accepted answer
  1. Viorel 88,571 Reputation points

    I think that you can define a member like this: event_token et;

    Then you can keep the result: et = lvMouseKeys().SelectionChanged({ this, &MainWindow::lvMouseKeys_SelectionChanged });

    To disable the handler: lvMouseKeys().SelectionChanged( et ).

    However, you can simply use a boolean flag at the beginning of the handler to skip the code.

1 additional answer

Sort by: Newest
  1. youki 886 Reputation points

    But that works great and that seems much simpler and clearer to me?!
    I somehow can't find the parameters for SelectionChanged for AddHandler, so I can then apply the RemoveHandler as well. Maybe I'm talking nonsense again?! I read a bit about the event_token I think.


    I also did that in WindowsForms with the handlers and then I recently saw/read that I could also do it with a flag. In addition, you can also use the Click event instead of the SelectionChanged, then of course it would be good if you could execute the click event via code, unfortunately I didn't find it, something like PerformClick (?!). It's not that easy...