IgnaceMaes / MaterialSkin

Theming .NET WinForms, C# or VB.Net, to Google's Material Design Principles.
MIT License
2.91k stars 838 forks source link

ShowDialog() not returning after form fully loading #254

Open jbradleyisland opened 4 years ago

jbradleyisland commented 4 years ago

Hello, thank you for the work you've done in producing and maintaining this package - it has made designing with winforms much more pleasant and enjoyable.

I'm having an issue with having a MaterialForm return from form.ShowDialog() in my application - basically I have two MaterialForms, one is a login form and the other is the main application. When I call the login form everything loads up cleanly but upon this.Close() nothing is returning to the main function:

`DialogResult result;

using (LoginForm1 lf = new LoginForm1()) result = lf.ShowDialog();

if (result == DialogResult.OK) { Application.Run(new LibraryForm()); } else { Application.Exit(); }`

I've testing my code using standard a winform form with standard controls without issue. I think it is related to form disposal but I'm not certain how to check for this. Please advise if there is any other pertinent information I may be able to provide to help determine the issue here. Thanks!

falcomus commented 3 years ago

I think this problem is because: Your LoginForm is the application MainForm. If it is closed, there is no open form a moment and the application terminates before you can launch the new LibraryForm.

One solution is similar when creating an application with SplashScreen.

Write in your "Program.cs" :

static class Program
{
    //Your mainform
    private static MainForm mainForm;

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //Create a MainForm  but hide it for now...
        mainForm = new MainForm();
        mainForm.Visible = false;

        //Comment this line, your MainForm will be showb later
        //Application.Run(new MainForm());

    }

}

Write in your MainForm constructor:

   public MainForm()
    {
        InitializeComponent();

        LoginForm loginForm = new LoginForm();

        if (loginForm.ShowDialog() == DialogResult.OK)
        {
            MessageBox.Show("OK, launching application");
            Application.Run(this);
        }

        else
        {
            MessageBox.Show("Cancel, terminating application");
            this.Close();  //Closes (still invisible) MainForm and automatically exits application.
        }

    }

Best regards Claus