简体   繁体   中英

c# Bring Folder Browser Dialog to Foreground

So I have a program that when it starts up for the first time it will open up a Folder Browser Dialog to search for a path needed. After the user has picked the path they hit ok and then a second Folder Browser Dialog opens up for the second path needed. The issue is that the second one does not appear in the foreground, its behind many other applications such as Chrome, Outlook, Visual Studio, etc. is there a way to bring it to the front?

code is as follows:

if (fbdCastPath.ShowDialog() == DialogResult.OK) { pathToCast = fbdOCASTPath.SelectedPath; } if (fbdConfig.ShowDialog() == DialogResult.OK) { pathToImg = fbdConfig.SelectedPath; }

when it starts up for the first time it will open up a Folder Browser Dialog

The "first time" is the hint to the cause. It is troublesome, you are probably doing this in the Main() method or a Load event handler, before your main window can be visible. You will still see the first dialog, Windows gives a starting program an opportunity to push a window into the foreground.

But you'll run into trouble when you close the first dialog. Windows now needs to give to focus to another window, your app doesn't have any that can be activated. It is still in limbo, waiting for your method to finish. It is not nearly done, still another dialog to display. So it needs to pick another window, that will be one that's owned by another application. Your second dialog now appears, underneath that window. High odds that you don't see it at all, there likely is no taskbar button either.

It should be clear now what you need to do, you must have a window in your app that can be activated to prevent losing the foreground. Or to be more precise, a dialog requires an owner window, one that it can give the focus back to when it closes. Use your main form's Shown event instead. Or use the boilerplate File + Open command.

Repro code:

    protected override void OnLoad(EventArgs e) {
        folderBrowserDialog1.ShowDialog();
        folderBrowserDialog2.ShowDialog();   // can't see this one
        base.OnLoad(e);
    }

Fixed by moving it into OnShown() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM