Question

I need to show a folder dialog box so the user can select a path before my application runs. I have everything working fine but I can't seem to be able to keep the error MessageBox on the foreground. If the user selects the wrong path a message box will pop up but it will stay in the background behind any open window on the desktop.

I'm new to WPF, with the winforms version of this application I could specify fdb.ShowDialog(this) and it would keep the error messagebox on the foreground. But with WPF the message box window always stays behind all other open windows.

Any ideas on how I could solve this? Thanks.

    while (!found)
    {
        if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if ((File.Exists(Path.Combine(fbd.SelectedPath, "user.exe"))))
                return fbd.SelectedPath;
            else                   
                System.Windows.Forms.MessageBox.Show("Cannot find user.exe in the selected path! Please try again.", "File Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

        }
    }
Was it helpful?

Solution

Move your code to Window_Loaded event instead of calling it from constructor -

<Window Loaded="Window_Loaded"/>

Code behind -

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Your code here
}

Since, your window is not loaded yet as the code execution haven't get passed the constructor yet and in the meanwhile error message pops up. So, once the window gets loaded it will comes over the message box.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top