1

I open a FolderBrowserDialog from a Winforms application. The first time after app start that works fine. Then i start a backgroundworker and do some work.

If i then, after the Backgroundworker finished, open the FolderBrowserDialog again, the app is "locked" because the FolderBrowserDialog is open but hidden somewhere in the background. I have to press the ALT key to make the Dialog visible.

The problem must have to do something with the backgroundworker... How can i solve this issue?

Here is the code where i open the Dialog:

private void metroButtonFolderBrowser_Click(object sender, EventArgs e) {

        FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
        folderBrowserDialog1.Description = "CD Importordner wählen";
        folderBrowserDialog1.ShowNewFolderButton = false;


        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            importfolder = folderBrowserDialog1.SelectedPath;
            ImportfolderLabelText.Text = importfolder;

        }
        else if (result == DialogResult.Cancel)
        {
            MessageBox.Show("Abbruch gewählt!");
            log.Info("User interrupted folder browser dialog.");
        }

    }
Christoph
  • 11
  • 2
  • How are you opening the dialog window? Are you reusing the same one or creating a new one? We're going to have a hard time helping you without seeing your code. – itsme86 Oct 05 '17 at 16:03
  • 1
    Show some code. For a start do you use `.Show()` or `.ShowDialog()` on the `FolderBrowserDialog`? Do you call this on the UI thread or in the BackgroundWorker? – Equalsk Oct 05 '17 at 16:04
  • I added the code now. I call the Dialog from the UI thread, after the backgroundworker did the work. – Christoph Oct 05 '17 at 16:31

1 Answers1

0

You can't set TopMost directly but you can provide the FolderBrowserDialog with a parent that is topmost:

using System;
using System.Windows.Forms;

private void BrowserForDirectory()
{
    FolderBrowserDialog dirDialog = new FolderBrowserDialog();

        using (var dummy = new Form() { TopMost = true })
        {
            if (dirDialog.ShowDialog(dummy.Handle))
            {
                importfolder = dirDialog.SelectedPath;
            }
        }
}
wecky
  • 754
  • 9
  • 17