954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# Question from beginning C# student

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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Button Form" Height="300" Width="400">
    <Grid>
        <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>
    </Grid>
</Window>

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()
        {
            InitializeComponent();
        }

        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,

Rod

rodce
Newbie Poster
6 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 
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".

HTH

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: