Asked  2 Years ago    Answers:  5   Viewed   8.2k times

How one can show dialog window (e.g. login / options etc.) before the main window?

Here is what I tried (it apparently has once worked, but not anymore):


<Application ...


public partial class App : Application
    private void Application_Startup(object sender, StartupEventArgs e)
        Window1 myMainWindow = new Window1();
        DialogWindow myDialogWindow = new DialogWindow();

Outcome: myDialogWindow is shown first. When it is closed, the Window1 is shown as expected. But as I close Window1 the application does not close at all.



Here's the full solution that worked for me:

In App.xaml, I remove the StartupUri stuff, and add a Startup handler:

<Application x:Class="MyNamespace.App"

In App.xaml.cs, I define the handler as follows:

public partial class App
    private void ApplicationStart(object sender, StartupEventArgs e)
        //Disable shutdown when the dialog closes
        Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

        var dialog = new DialogWindow();

        if (dialog.ShowDialog() == true)
            var mainWindow = new MainWindow(dialog.Data);
            //Re-enable normal shutdown mode.
            Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
            Current.MainWindow = mainWindow;
            MessageBox.Show("Unable to load data.", "Error", MessageBoxButton.OK);
Sunday, September 4, 2022

In your MainWindow.xaml.cs, try doing this:

protected override void OnClosed(EventArgs e)


Per this link, you can also set the ShutdownMode in XAML:


Applications stop running only when the Shutdown method of the Application is called. Shut down can occur implicitly or explicitly, as specified by the value of the ShutdownMode property.

If you set ShutdownMode to OnLastWindowClose, Windows Presentation Foundation (WPF) implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow).

A ShutdownMode of OnMainWindowClose causes WPF to implicitly call Shutdown when the MainWindow closes, even if other windows are currently open.

The lifetime of some applications may not be dependent on when the main window or last window is closed, or may not be dependent on windows at all. For these scenarios you need to set the ShutdownMode property to OnExplicitShutdown, which requires an explicit Shutdown method call to stop the application. Otherwise, the application continues running in the background.

ShutdownMode can be configured declaratively from XAML or programmatically from code.

This property is available only from the thread that created the Application object.

In your case, the app isn't closing because you're probably using the default OnLastWindowClose:

If you set ShutdownMode to OnLastWindowClose, WPF implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow).

Since you're opening a new window, and not closing it, shutdown doesn't get called.

Saturday, October 15, 2022

You can try to get a hold of the MainWindow in the Loaded event like this

private void Window_Loaded(object sender, RoutedEventArgs e)
    Application curApp = Application.Current;
    Window mainWindow = curApp.MainWindow;
    this.Left = mainWindow.Left + (mainWindow.Width - this.ActualWidth) / 2;
    this.Top = mainWindow.Top + (mainWindow.Height - this.ActualHeight) / 2;
Wednesday, August 31, 2022

I assume that you are referring to WPF not Windows Form Here is example of using SaveFileDialog

//configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; //default file name
dlg.DefaultExt = ".xml"; //default file extension
dlg.Filter = "XML documents (.xml)|*.xml"; //filter files by extension

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process save file dialog box results
if (result == true)
   // Save document
   string filename = dlg.FileName;

Other Example:

In WPF you have to handle conflict between DialogResult Enumeration and Window.DialogResult Property

Try using fully qualified name to refer the enumeration:

System.Windows.Forms.DialogResult result = dlg2.ShowDialog();

if (result == DialogResult.OK)
Tuesday, October 4, 2022

pass a parameter to the loginwindow of type MainWindow. That allows the Login window to have a reference to the MainWindow:

Login li = new Login(this);

And the login window:

private MainWindow m_parent;
public Login(MainWindow parent){
    m_parent = parent;

//Login Succesfull function

private void Succes(){
Monday, October 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages