I'm having a problem solving part of a C# question from a beginning course. If anyone can help, I would appreciate it. The problem and code follows:

When the user clicks the Quit button, display a message box that says "I'm going home." Then when the user clicks OK on the message box, have it display the same message box as the "OK" or "Do It" button on your main form. This part of the problem will not use delegates directly, it will exercise two ways to cause one event handler to handle two (or more) events.

<Window x:Class="AdvancedProj2Version2.Window1"
    Title="Button Form" Height="300" Width="400">
        <Button HorizontalAlignment="Left" Margin="38,108,0,131" Name="btnOK" Width="75" Click="btnOK_Click">OK</Button>
        <Button Margin="150,108,0,131" Name="btnDoIt" HorizontalAlignment="Left" Width="76" Click="btnOK_Click">Do It</Button>
        <Button HorizontalAlignment="Right" Margin="0,108,42,131" Name="btnQuit" Width="75" Click="btnQuit_Click_1">Quit</Button>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AdvancedProj2Version2
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
        public Window1()

        private void btnOK_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("Good Bye");

        private void btnQuit_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("I'm going home");

If need more information don't hesitate to contact me.

Best Regards,


6 Years
Discussion Span
Last Post by Teme64

it will exercise two ways to cause one event handler to handle two (or more) events.

You need to know which button is pressed? Check the sender object's name property. Here's a WinForms example of (button click) event handler that checks which button is pressed

private void buttonClick(object sender, EventArgs e)
    if (((Button)sender).Name == "button1")
        MessageBox.Show("button1 Pressed");
    if (((Button)sender).Name == "button2")
        MessageBox.Show("button2 Pressed");

Your code will be similar but the button names are "btnOK" and "btnDoIt".


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.