Christoph
Christoph

Reputation: 11

FolderBrowserDialog hidden in background

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.");
        }

    }

Upvotes: 1

Views: 800

Answers (1)

wecky
wecky

Reputation: 910

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

Upvotes: 0

Related Questions