views:

72

answers:

3

I've gotten my brain in a knot over the simple task of getting an application directory from the user. I have an AppFolderDialog form, which I use as a dialogue, on which there are OK and Cancel buttons, a read only textbox, and a Browse button. The browse button opens up a FolderBrowserDialog.

Now when the user clicks OK, I need a loop to check if the selected directory contains a certain file, and give the user a Cancel or Retry message box. Cancel will forward the cancel to the dialogue to close it and return DialogResult.Cancel. Retry will simply give the user another chance to browse for a directory.

Now I also need a Cancel button on the dialogue itself, so the user can cancel without having to select an invalid directory. I know I have this all wrong, but I'm busy with many things at once, and my concentration is shot. I would appreciate some suggestions as to how to improve this task in the application.

+1  A: 

I would suggest that you not tie the two dialogs together. Have the first dialog just obtain the proper directory. Once the user clicks ok the first dialog is dismissed, you check the result in your application and if the expected file doesn't exist, pop up an error dialog informing them that the chosen directory was not correct. You could give them the option of retrying, which would just pop up the first dialog again. Canceling out of either dialog would simply result in not having chosen a directory.

tvanfosson
I like your train of thought, and I think it's inspired me to not even use my own dialog. Just let the user browse folders from the main form, with the same retry loop.
ProfK
+1  A: 

Note that you will still have to have code that that checks if the directory exists when you actually try to do something with it.

Seeing as you have to have that code anyway, I wouldn't bother checking in the directory select dialog. Let the user select a directory and if they somehow select one that doesn't exist, or doesn't have your app data in it or whatever, put off the error message. After all, the user could select a perfectly good directory in your app and then delete it.

anon
A: 

Is this what your looking for?

    private string GetPathFromUser()
    {
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        while (dialog.ShowDialog() == DialogResult.OK)
        {
            // Do your validation here
            bool pathIsGood = false;

            if (pathIsGood)
            {
                return dialog.SelectedPath;
            }
            else
            {
                DialogResult cancelRetry = MessageBox.Show("Directory is not valid becuase bla..", "", MessageBoxButtons.RetryCancel);
                if (cancelRetry == DialogResult.Cancel) break;
            }
        }
        return null;
    }
tarn