A
A
Alexey FRZ2018-09-26 16:00:44
.NET
Alexey FRZ, 2018-09-26 16:00:44

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

3 answer(s)
#
#, 2018-09-26
@leshqow

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

3 - I won’t tell you for WPF, but for sure you can consider both approaches and try to look for analogies
ps for WinForms
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

F
Floydreme, 2018-09-26
@FloydReme

I did like this

private void Login_Click(object sender, RoutedEventArgs e)
{
      this.Hide();
      MainPage mp = new MainPage();
      mp.Show();
}

G
GrWizard, 2019-05-07
@GrWizard

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 question

Ask a Question

731 491 924 answers to any question