Cannot use DialogResult
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.
Related videos on Youtube
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, 2022Comments
-
KMC almost 2 years
I tried to use
DialogResult
to check anMessagebox
'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 almost 13 yearsI think you're mixing up WPF and Winforms code. Check this link for more information. And I think you're looking for
MessageBoxResult
rather thanDialogResult
.
-
-
Jodrell almost 13 yearsor rather, use
var
to hide a problem and avoid having to understand