Forum guidelines
Responding to DialogResult RetryCancel
Posted By Richard Arnold on 01 Jan 2013
responding to dialogresult My application allows the user to select a particular file to import from a ToolStripDropDownButton. The code then displays an OpenFileDialog for them to browse to the file to import.
As the file actually chosen may be different from the selected ToolStripDropDownButton item, I want to  prevent an incorrect file from being copied. I wrote a method which compares the two selections and displays a RetryCancel DialogResult in a MessageBox.

If the user clicks "Retry", I want to return to the calling code's OpenFileDialog for the user to make another file selection.

If the user clicks "Cancel", I do Not want to exit the application, just allow the user to continue using the application.

How do I return to the calling code's OpenFileDialog based on the "Retry" button and how can I handle the "Cancel" button?

I have attached the simple method example. Thank you in advance.

  • 1
  • 0
Re: Responding to DialogResult RetryCancel
Posted By Vulpes on 02 Jan 2013   Accepted Answer
It's difficult to answer this question without seeing the calling code but, if I were you, I'd just let this method check whether the user file selection is acceptable and let the calling code deal with the repercussions of it not being.

You could therefore just reduce the method to this, returning a bool:

private bool ConfirmCorrectFileSelection(string myFile, string pubType) // Confirm user file selection matches the ToolStripDropDown choice 
   if (myFile.Contains("Some selected text") & pubType == "Similar text") return true;
   return false;

I'd then place the code which calls the OpenFileDialog in a while(true) loop and call the above method from within the loop. You'd then end up with something like this:

   // code to call OpenFileDialog

   if (ConfirmCorrectFileSelection(myFile, oubType))
      break; // no need to call OpenFileDialog again
      string message = "You chose to import a file but selected a different file. Please correct your selection.";
      string caption = "Your File Selection is INCORRECT!";
      MessageBoxButtons buttons = MessageBoxButtons.RetryCancel;
      DialogResult result;
      //Display the MessageBox.
      result = MessageBox.Show( message, caption, buttons );
      if (result == DialogResult.Cancel) 
         // continue operation
         e.Cancel = true; // if needed, see below
      // otherwise Retry must have been pressed and loop will continue and OpenFileDialog will be called again

Re: Responding to DialogResult RetryCancel
Posted By Richard Arnold on 05 Jan 2013  
Hello Vulpes, Sorry I didn't respond sooner. Couldn't get back to my code for a few days.

Your solution was exactly what I needed. I actually had 4-different files to evaluate.

Using your suggestion with a slight modification of a Ternary statement, I am able to confirm whether the users initial file-import choice matches their actual file selection and if not, to display a warning along with the FileDialog again.

Thank you very much for your consideration in this matter. It is appreciated.

Download Free eBooks


Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.