Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the result from a Yes/No MessageBox?

Tags:

c#

wpf

messagebox

I'm trying to make a program which reserves and shows available and unavailable seats for an "event". The errors I'm getting are as follows:

'System.Nullable' does not contain a definition for 'Yes' and no extension method 'Yes' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)

(Same goes for "No") and

'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'.

This is what I have so far:

private void btnSeat1_Click(object sender, RoutedEventArgs e)
{
    if (!Seat1)
    {
        DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);

        if (Result == DialogResult.Yes)
        {
            MessageBox.Show("You Reserved this seat");
            btnSeat1.Text = "Reserved";
        }
        else if (Result == DialogResult.No)
        {
            Environment.Exit(0);
        }

Note: I only used Environment.Exit as a placeholder. It was intentional and will be changed accordingly. It isn't the source of the problem.

like image 827
Nathan Avatar asked Dec 04 '22 01:12

Nathan


1 Answers

This line:

DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);

Should actually be this:

var Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);

And then if you hover over Result you can see its not of a DialogResult, but a MessageBoxResult, or, if you prefer to explicitly type it, try:

MessageBoxResult Result = MessageBox.Show(...

So you would use it like this in your if statements:

if (Result == MessageBoxResult.Yes)
{
    MessageBox.Show("You Reserved this seat");
    btnSeat1.Text = "Reserved";
}
else if (Result == MessageBoxResult.No)
{
    Environment.Exit(0);
}

You are getting the error because DialogResult is actually a property of the Window class, and you are trying to use it like its a type (as the compiler says).

like image 61
Ron Beyer Avatar answered Dec 22 '22 16:12

Ron Beyer