【问题标题】:C# - OpenFileDialog opening in the same Form/TabC# - OpenFileDialog 在同一个表单/选项卡中打开
【发布时间】:2017-10-26 21:20:11
【问题描述】:

当我运行 .ShowDialog() 时,它会运行并正常打开,但它会以相同的表单/选项卡而不是新的表单/选项卡打开。当我单击任务栏中的应用程序图标以简单地打开表单时,这会导致问题,而 OpenFileDialog 隐藏在应用程序和其他窗口后面,导致应用程序基本上被冻结。

唯一的解决方法是慢慢关闭所有其他应用程序(最小化),然后我可以单击 OpenFileDialog 并继续操作。

string proxyFile = "";
Thread thread = new Thread(() =>
{
    OpenFileDialog _ofd = new OpenFileDialog();
    _ofd.Filter = "txt|*.txt";
    using (OpenFileDialog ofd = _ofd)
        if (ofd.ShowDialog() == DialogResult.OK && ofd.CheckFileExists)
        {
            proxyFile = ofd.FileName;
        }
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join(); // Freeze until dialog is done, then it exits the thread and continues with the filepath.
MessageBox.Show(proxyFile);

我必须在线程下运行它,因为它不会在 STA 中执行(使用 CEFSharp JSCallback 执行)所以我必须使用线程作为解决方法。

【问题讨论】:

  • ofd 是需要 STA 的 Windows Shell 的包装器。你是什​​么意思它没有在 STA 中执行?

标签: c# openfiledialog


【解决方案1】:

最后我的解决方法是使用“ActiveForm”中的方法调用,并且在 ofd.ShowDialog() 方法中它有一个添加表单/句柄的参数,所以我将其更改为 ofd.ShowDialog(ActiveForm) 和效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多