How to remove/ deactivate an event?

youki 846 Reputation points
2023-01-06T17:32:46.807+00:00

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

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

https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/handle-events

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.
438 questions
C++
C++
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,633 questions
No comments
{count} votes

Accepted answer
  1. Viorel 82,911 Reputation points
    2023-01-06T18:17:08.977+00:00

    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.

    No comments

1 additional answer

Sort by: Most helpful
  1. youki 846 Reputation points
    2023-01-06T18:50:44.487+00:00

    Wow,
    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.

    PS:

    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...

    No comments