DialogResult.OK on SaveFileDialog not work

别等时光非礼了梦想. 提交于 2019-12-30 22:56:06

问题


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'


回答1:


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)
            {....}



回答2:


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

DialogResult result = dlg2.ShowDialog(); 
if (result == System.Windows.Forms.DialogResult.OK)
                {....}


来源:https://stackoverflow.com/questions/23539219/dialogresult-ok-on-savefiledialog-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!