Search code examples
c#wpfmessagebox

MessageBoxButton.YesNo in object


The code that I cant get to work:

object MessageBoxButton = null;  
if (MessageBox.Show(String.Format("{0:0,0}", Convert.ToInt32(txtQuantity.Text)), "OK ??????", MessageBoxButton.YesNo, MessageBoxImage.Question) == DialogResult == false)

Links to stuff I found/tried:

Link1

Link2

The error I get: 'object' does not contain a definition for 'YesNo' and no extension method 'YesNo'.


Solution

  • Why do you try to declare a local variable named MessageBoxButton? This is the name of class that already exists in the framework. If the compiler says that it cannot find the type MessageBoxButton, you have to include the namespace where it can be found using a using clause at the top of the file:

    using System.Windows;
    

    Then you can just write:

    public class Class1
    {
        public static void Demo()
        {
            MessageBoxResult dialogResult = MessageBox.Show("Text", "Caption", MessageBoxButton.YesNo, MessageBoxImage.Information);
            if(dialogResult == MessageBoxResult.Yes)
            {
                MessageBox.Show("Yes was clicked");
            }
            else 
            {
                MessageBox.Show("No was clicked");
            }
        }
    }
    

    Note that the Microsoft Styleguide says not to use MessageBoxImage.Question. Only use Information, Warning or Error instead (or no Icon at all).