9

I tried to use DialogResult to check an Messagebox's YesNoCancel. I'm using the following code which I don't see any problem with:

DialogResult dlgResult = MessageBox.Show(
   "Save changes before closing?", 
   "Warning", 
   MessageBoxButton.YesNoCancel, 
   MessageBoxImage.Question);

But Visual Studio throws me error saying

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

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
KMC
  • 19,548
  • 58
  • 164
  • 253
  • 7
    I think you're mixing up WPF and Winforms code. Check [this](http://marlongrech.wordpress.com/2008/05/28/wpf-dialogs-and-dialogresult/) link for more information. And I think you're looking for `MessageBoxResult` rather than `DialogResult`. – Hans Olsson Jun 07 '11 at 12:17

6 Answers6

10

There is a confliction here between the DialogResult Enumeration and the Window.DialogResult Property.

To solve this problem, you can use the fully qualified name of the enumuration. As the following:

System.Windows.Forms.DialogResult dlgResult = ...

However, since you are using WPF, use MessageBoxResult Enumeration to get the result of the message:

MessageBoxResult result = 
    MessageBox.Show("Would you like to see the simple version?", 
    "MessageBox Example", MessageBoxButton.OKCancel);
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
1

Try to declare dlgResult as var. Then it should work

    var dlgResult = 
        MessageBox.Show("Save changes before closing?", 
            "Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

Also MessageBox.Show under WPF does return MessageBoxResult and not DialogResult. DialogResult is used in WindowsForms.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
Jehof
  • 34,674
  • 10
  • 123
  • 155
1

The problem is DialogResult is also a property of the form and the compiler thinks you are refering to this property.

You have several choices here:

  • Use the fully qualified name of the type System.Windows.Forms.DialogResult
  • Use var to let the compiler figure out the type and get rid of the name collision
Johann Blais
  • 9,389
  • 6
  • 45
  • 65
1

DialogResult is not a type, its a property, you want the type MessageBoxResult

I can see from the question you are not using winforms. So the code would read,

MessageBoxResult result = MessageBox.Show(
    "Save changes before closing?",
    "Warning",     
    MessageBoxButton.YesNoCancel,
    MessageBoxImage.Question);
Jodrell
  • 34,946
  • 5
  • 87
  • 124
1

just try with MessageBoxResult

MessageBox will return MessageBoxResult enum values

            MessageBoxResult dlgResult = MessageBox.Show("Save changes before closing?","Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
        Console.WriteLine(dlgResult);
Kishore Kumar
  • 21,449
  • 13
  • 81
  • 113
1
MessageBoxResult result = MessageBox.Show(
"Save changes before closing?",
"Warning",     
MessageBoxButton.YesNoCancel,
MessageBoxImage.Question);

then use result to check

Hema Elpop
  • 11
  • 2