0

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

        }
    }
Mark S.
  • 45
  • 1
  • 5
  • Have you placed the above code in your constructor of Window/UserControl..? – Rohit Vats Nov 25 '12 at 16:13
  • @RV1987 Not in the constructor, it is part of a method "GetUserPath()" that is called from the main Window constructor. I tested with all the code in the constructor but there's no change in the MessageBox behavior. – Mark S. Nov 25 '12 at 16:25
  • Yeah that's the point since you are calling the code from constructor and till that time window is not created. Window gets loaded once code go past your constructor. – Rohit Vats Nov 25 '12 at 16:41

1 Answers1

1

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.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • That solved the problem! Thank you for your help! I obviously need a lot more practice with WPF! – Mark S. Nov 25 '12 at 18:05