如何多次显示FolderBrowserDialog?

发布于 2024-09-06 01:59:06 字数 765 浏览 6 评论 0原文

在我的Windows窗体的Form_Load事件中,我想显示一个FolderBrowserDialog来让用户选择一个目录,如果他们选择的目录无效(意味着它缺少应用程序需要的某些文件),我想再次显示它。但是,当我创建一个新的FolderBrowserDialog时,当我调用ShowDialog时它不会出现。

while (ValidDirectorySelected() == false && tryAgain == true)
{
 using (FolderBrowserDialog dialog = new FolderBrowserDialog())
 {
  dialog.ShowNewFolderButton = false;

  if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
  {
   tryAgain = false;
  }
 }
}

当我进入它时,第二次到达dialog.ShowDialog()行,然后什么也没有发生。该对话框不会出现,调试器也不会继续。它就停止了。第一次效果很好,但第二次就不行了。我什至尝试复制整个 using 块并将其粘贴到第一个块之后,并且发生同样的事情。该对话框仅显示一次。

我需要做什么才能多次显示FolderBrowserDialog?

解决方案:

将“this”传递给 ShowDialog 解决了我的问题。我还将 using 移到了 while 循环之外,以避免不必要地重新创建对话框。

In my Windows Form's Form_Load event, I want to show a FolderBrowserDialog to let the user select a directory, and if the directory they've selected is not valid (meaning it lacks certain files that the application needs), I want to show it again. However, when I create a new FolderBrowserDialog, it does not appear when I call ShowDialog.

while (ValidDirectorySelected() == false && tryAgain == true)
{
 using (FolderBrowserDialog dialog = new FolderBrowserDialog())
 {
  dialog.ShowNewFolderButton = false;

  if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
  {
   tryAgain = false;
  }
 }
}

When I step into it, the dialog.ShowDialog() line is reached on the second time, and then nothing happens. The dialog does not appear, and the debugger doesn't move on. It just stops. It works perfectly the first time, but not the second. I've even tried just copying that entire using block and pasting it right after the first one, and the same thing happens. The dialog shows only once.

What do I need to do to show a FolderBrowserDialog more than once?

Solution:

Passing 'this' to ShowDialog fixed my issue. I also moved the using to outside of the while loop, to avoid needlessly re-creating the dialog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2024-09-13 01:59:06

最小化 Visual Studio,您会发现对话框回来了。

这是一个焦点问题,由于您在 Load 事件中显示对话框而触发。当对话框关闭时,应用程序中不再有可以接收焦点的窗口。您的 Load 事件尚未完成运行,因此应用程序的主窗口尚不可见。 Windows 必须找到一个窗口来给予焦点,并从另一个程序中选择一个窗口。就像视觉工作室一样。

当您再次显示该对话框时,它无法夺回焦点,因为 Visual Studio 已获取焦点。因此该对话框出现在 Visual Studio 主窗口后面,看不见。

您必须通过允许主窗口变得可见来解决此问题。并调用dialog.ShowDialog(this)来完全确定。例如,您可以使用 Shown 事件。

Minimize Visual Studio, you'll find the dialog back.

This is a focus issue, triggered because you display the dialog in the Load event. When the dialog closes, there is no window left in your app that can receive the focus. Your Load event hasn't finished running so the app's main window isn't yet visible. Windows has to find a window to give the focus to and will select one from another program. Like Visual Studio.

When you display the dialog again, it cannot steal the focus back because Visual Studio has acquired it. So the dialog appears behind Visual Studio's main window, out of view.

You'll have to fix this by allowing your main window to become visible. And call dialog.ShowDialog(this) to be completely sure. You could use the Shown event, for example.

冷月断魂刀 2024-09-13 01:59:06

试试这个:

using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
    while (ValidDirectorySelected() == false && tryAgain == true)
    {
        dialog.ShowNewFolderButton = false;

        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
        {
            tryAgain = false;
        }
    }
}

...将您的使用移到 while 循环之外,以免每次都破坏文件夹浏览器。你不必这样做。您可以重用FolderBrowserDialog。

Try this:

using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
    while (ValidDirectorySelected() == false && tryAgain == true)
    {
        dialog.ShowNewFolderButton = false;

        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
        {
            tryAgain = false;
        }
    }
}

...move your using outside the while loop to keep from destroying the folder browser every time. You don't have to do that. You can reuse FolderBrowserDialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文