locked
Adding to event handlers RRS feed

  • Question

  • Hi,

    I'm starting with the book "Head Firs C#" with a WPF app. 

    I need two add two event handlers to a "Canvas" but when I add the first event handler from the properties panel I have no more choices to add the second one.
    Is it a normal behaviour?
    Sunday, November 8, 2020 7:33 AM

Answers

  • Hi everybody,

    I found this in github:

    https://github.com/head-first-csharp/third-edition/blob/master/WPF/Chapter_1/Save_the_Humans/Save_the_Humans/MainWindow.xaml

    I copied the line from the MainPage.xaml which could explain the solution.

    <Canvas x:Name="playArea" Grid.ColumnSpan="3" MouseMove="playArea_MouseMove" MouseLeave="playArea_MouseLeave">

    • Marked as answer by Thrasso00 Tuesday, November 10, 2020 6:06 AM
    Monday, November 9, 2020 6:05 AM

All replies

  • Do you want to add the second handler for the same event or for a different event?

    The second handler for the same event can be added programmatically using “+=” operator, which is probably explained in the book.

    Sunday, November 8, 2020 8:22 AM
  • Hi,

    it is a normal behavior, in the properties Window you select only one event, but if you take a look to for example yourWindow.xaml.cs you'll see that first: it is a partial class which means that a part of its definition can be in other files, second thing: if you right click on the InitializeComponent() call and select go to definition you'll find the definitions and declarations of the controls you have in the window/view, from here try to locate your Canvas declaration for example:  

    Canvas.EventName += OnEventName;

    where EventName is the event you subscribed to and OnEventName is the action, now you can register more actions to handle the event, here is an example:

    Canvas.ContextMenuOpening += OnContextMenuOpening_1st_handler;
    
    Canvas.ContextMenuOpening += OnContextMenuOpening_2nd_handler;
    
    // etc ...
    

    hope it helps!

    best regards.

      
    Sunday, November 8, 2020 9:42 AM
  • Hi Thrasso00,

    Thank you for posting here.

    In c#, events and handlers are not a one-to-one relationship.

    An event can correspond to multiple handlers, as mentioned, this is usually called multicast, please check the example provided by Microsoft:

    How to combine delegates (Multicast Delegates) (C# Programming Guide)

    Sometimes multiple events correspond to one handler, like this:

            public MainWindow()
            {
                InitializeComponent();
    
                button.Click += Button_Click;
                button1.Click += Button_Click;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show((e.OriginalSource as Button).Name);
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 9, 2020 1:54 AM
  • Hi everybody,

    I found this in github:

    https://github.com/head-first-csharp/third-edition/blob/master/WPF/Chapter_1/Save_the_Humans/Save_the_Humans/MainWindow.xaml

    I copied the line from the MainPage.xaml which could explain the solution.

    <Canvas x:Name="playArea" Grid.ColumnSpan="3" MouseMove="playArea_MouseMove" MouseLeave="playArea_MouseLeave">

    • Marked as answer by Thrasso00 Tuesday, November 10, 2020 6:06 AM
    Monday, November 9, 2020 6:05 AM
  • Hi,

    It seems that we all got you wrong at first. :)

    But in any case, the problem is solved.

    please click on the "Mark as answer" option of your own reply, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 9, 2020 9:20 AM