DialogResult.OK on SaveFileDialog not work
Asked Answered
J

4

7

I try, when I press save in SaveFileDialog I do something. I trying fix but always something wrong.

SaveFileDialog dlg2 = new SaveFileDialog();
dlg2.Filter = "xml | *.xml";
dlg2.DefaultExt = "xml";
dlg2.ShowDialog();
if (dlg2.ShowDialog() == DialogResult.OK)
{....}

But I have error on OK - which say:

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

I try fix with this code:

DialogResult result = dlg2.ShowDialog(); //here is error again
if (result == DialogResult.OK)
                {....}

Now error is on DialogResult say: 'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'

Jollification answered 8/5, 2014 at 10:30 Comment(2)
If it WPF or Winforms?Unlace
See https://mcmap.net/q/1156204/-cannot-use-dialogresult . Same issueAbbottson
M
17

I assume that you are referring to WPF not Windows Form Here is example of using SaveFileDialog

//configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; //default file name
dlg.DefaultExt = ".xml"; //default file extension
dlg.Filter = "XML documents (.xml)|*.xml"; //filter files by extension

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process save file dialog box results
if (result == true)
{
   // Save document
   string filename = dlg.FileName;
}

Other Example:

In WPF you have to handle conflict between DialogResult Enumeration and Window.DialogResult Property

Try using fully qualified name to refer the enumeration:

System.Windows.Forms.DialogResult result = dlg2.ShowDialog();

if (result == DialogResult.OK)
            {....}
Moll answered 8/5, 2014 at 10:50 Comment(0)
O
2

DialogResult return System.Windows.Forms.DialogResult.So u can use like that=>

DialogResult result = dlg2.ShowDialog(); 
if (result == System.Windows.Forms.DialogResult.OK)
                {....}
Orthodontist answered 21/2, 2019 at 8:29 Comment(0)
L
0

Instead of checking if dlg2.ShowDialog() is equal to DialogResult.OK just check if it is equal to true

if (dlg2.ShowDialog() == true)
{....}
Loophole answered 12/6, 2020 at 14:18 Comment(0)
E
0

This is a very old topic but I will give you the solution for it. The dialog result you're looking for (for savefiledialog) is .Yes or !Cancel so it looks something like this:

if (dlg2.ShowDialog() == DialogResult.Yes)

or

if (dlg2.ShowDialog() != DialogResult.Cancel)
Episode answered 19/9, 2020 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.