How to remove/ deactivate an event?

youki 1,026 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 development | Windows App SDK
Developer technologies | C++
Developer technologies | 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.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 126.3K 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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. youki 1,026 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...

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.