Answer the question
In order to leave comments, you need to log in
Best practics. How to close the main form from the child?
After starting the program, a window appears in the login and password entry field (LoginForms), when you click the login button (if the login and password are correct), another window opens with the program's functionality, and the window with the login and password closes.
What is the best solution here? The only thing that comes to my mind is closing the LoginForms after clicking on the login button.
Answer the question
In order to leave comments, you need to log in
1 - it is not necessary to make "login" the main form. you can start the main form hidden, and the child login form, but show it earlier, and then act depending on the success of the login
2 - specifically in WinForms, you can work out generally different forms sequentially
using System;
using System.Windows.Forms;
namespace X
{
internal static partial class Y
{
/// <summary>
/// Главная точка входа для приложения.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(A = new fmA());
// bla-bla-bla with login
Application.Run(B = new fmB());
}
internal static fmA A;
internal static fmB B;
}
}
How to close the main form from the child?the literal answer is no how , closing the main form is termination and if you don't modify the code then it's program termination
Application.Run(A = new fmXXX());
Main
I did like this
private void Login_Click(object sender, RoutedEventArgs e)
{
this.Hide();
MainPage mp = new MainPage();
mp.Show();
}
IMHO, but I use a self-written WindowManager, through which windows and tabs are managed in windows, if they are supported there. This is a very old problem, a window is a view, according to the mvvm logic, there should be no access from the view model to the window, so you cannot open or close the window from the view model. You can implement everything at the presentation layer, but who needs it? there is a manager and he is available to everyone.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question