浏览目录并取消,重试

时间:2022-06-11 11:29:26

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.

对于从用户获取应用程序目录的简单任务,我已经知道了我的大脑。我有一个AppFolderDialog表单,我将其用作对话框,其中有OK和Cancel按钮,只读文本框和Browse按钮。浏览按钮打开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.

现在,当用户单击“确定”时,我需要一个循环来检查所选目录是否包含某个文件,并为用户提供“取消”或“重试”消息框。取消会将取消转发到对话框以关闭它并返回DialogResult.Cancel。重试只会让用户有机会浏览目录。

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.

现在我还需要对话框本身的“取消”按钮,这样用户就可以取消而无需选择无效的目录。我知道我这一切都错了,但是我一下子忙着做很多事情,我的注意力集中了。对于如何在应用程序中改进此任务,我将不胜感激。

3 个解决方案

#1


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.

看到你必须拥有该代码,我不打扰在目录选择对话框中检查。让用户选择一个目录,如果他们以某种方式选择一个不存在的目录,或者没有你的应用程序数据或其他任何内容,请推迟错误消息。毕竟,用户可以在您的应用中选择一个完美的目录,然后将其删除。

#2


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.

我建议你不要将两个对话框绑在一起。让第一个对话框获取正确的目录。一旦用户单击“确定”,第一个对话框将被取消,您将检查应用程序中的结果,如果预期的文件不存在,则弹出一个错误对话框,通知他们所选目录不正确。你可以给他们重试的选项,这会再次弹出第一个对话框。取消任一对话框只会导致没有选择目录。

#3


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;
    }

#1


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.

看到你必须拥有该代码,我不打扰在目录选择对话框中检查。让用户选择一个目录,如果他们以某种方式选择一个不存在的目录,或者没有你的应用程序数据或其他任何内容,请推迟错误消息。毕竟,用户可以在您的应用中选择一个完美的目录,然后将其删除。

#2


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.

我建议你不要将两个对话框绑在一起。让第一个对话框获取正确的目录。一旦用户单击“确定”,第一个对话框将被取消,您将检查应用程序中的结果,如果预期的文件不存在,则弹出一个错误对话框,通知他们所选目录不正确。你可以给他们重试的选项,这会再次弹出第一个对话框。取消任一对话框只会导致没有选择目录。

#3


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;
    }