Cannot use DialogResult

11,645

Solution 1

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);

Solution 2

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

Solution 3

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);

Solution 4

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

then use result to check

Solution 5

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.

Share:
11,645

Related videos on Youtube

KMC
Author by

KMC

Tutorials: WPF C# Databinding C# Programming Guide .NET Framework Data Provider for SQL Server Entity Framework LINQ-to-Entity The Layout System Resources: LifeCharts: free .NET charts FileHelper: text file helper cmdow

Updated on July 27, 2022

Comments

  • KMC
    KMC almost 2 years

    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'

    • Hans Olsson
      Hans Olsson almost 13 years
      I think you're mixing up WPF and Winforms code. Check this link for more information. And I think you're looking for MessageBoxResult rather than DialogResult.
  • Jodrell
    Jodrell almost 13 years
    or rather, use var to hide a problem and avoid having to understand