• 0

Help with understanding whit error message for "FORMS"


Question

Hi, I am trying to write code for program that when it starts it will immediately ask user to save file, but code i put in System.Windows.Forms.DialogResult.Yes , in error list is showing me error message (The type or namespace 'Forms' doesn not exist in the namespace 'System.Windows'(are you missing an assembly reference?)).

 

My code is following:

 

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
            saveDlg.DefaultExt = ".rtf";
            saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
 
            RatHere:
            if (saveDlg.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
            {
                string filename = saveDlg.FileName;
                System.IO.File.Create(filename);
            }
            else
            {
                MessageBox.Show("Morate Snimiti Fajl prije pocetka programa!", "SAVE", MessageBoxButton.OK);
                goto RetHere;
            }
        }
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Microsoft.Win32.SaveFileDialog returns a nullable Boolean. You can't compare it to DialogResult.

You want

var saveOk = saveDlg.ShowDialog();
if(saveOk.HasValue && saveOk.Value)
{
  ...
}
else
{
  ...
}
Link to comment
Share on other sites

  • 0

Microsoft.Win32.SaveFileDialog returns a nullable Boolean. You can't compare it to DialogResult.

You want

var saveOk = saveDlg.ShowDialog();
if(saveOk.HasValue && saveOk.Value)
{
  ...
}
else
{
  ...
}

Thank you, this worked very well, and thank you for explanation. Can I ask you something else, with this code i want it for user to save file when program starts, it promtp me with saveFileDialog to save, but when user press on cancel it comes with error message "you have to save file!" but after click on OK it just takes me back to program and I want it to go back to option to save. Would you know how to make that happened?

Link to comment
Share on other sites

  • 0

Thank you, this worked very well, and thank you for explanation. Can I ask you something else, with this code i want it for user to save file when program starts, it promtp me with saveFileDialog to save, but when user press on cancel it comes with error message "you have to save file!" but after click on OK it just takes me back to program and I want it to go back to option to save. Would you know how to make that happened?

 

You need to call ShowDialog() again to show it. I'm guessing you have the label positioned after the ShowDialog() code.

 

Also a couple of things:

Link to comment
Share on other sites

  • 0

It should be noted that it's bad practice to force an infinite recursion of save dialogs. Cancel is cancel and should do exactly that, cancel the entire operation.

 

Agreed - a custom dialog with no 'Cancel' button should be used rather than simply reopening the dialog every time cancel is hit. That's just lazy and a poor user experience. 

Link to comment
Share on other sites

  • 0

Agreed - a custom dialog with no 'Cancel' button should be used rather than simply reopening the dialog every time cancel is hit. That's just lazy and a poor user experience. 

 

I don't agree with the idea of forcing the user to specify and save a file on start-up (that's just a poor user experience in general), but if the OP is going to do that, it doesn't make much difference if the OP just re-pops up the dialog or if the OP removes the cancel option altogether. Either case is bad design from an expectations view point and is going to throw the user off. Either way, the user is going to readily realize they can't actually cancel the dialog and be like 'wtc'.

Link to comment
Share on other sites

This topic is now closed to further replies.