User Switching

1

Good evening people I'm doing a project in the Faculty and I came across a problem at a time and I could not solve it. I have the Login screen and within the system I have User Change and when I call the Login screen it opens another system with what was being used underneath, when I close the previous application it closes everything, I made the authentication close the current screen and opens a new one that when closing the previous application is waiting for confirmation of closing yes or is there anything I can solve this?

The code below is the menu button calling the user login screen, the commented code is the test I did with an internet example but it did not work out

code:

    private void trocarUsuarioToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmLogin objLogin = new frmLogin();
        objLogin.MdiParent = this;
        objLogin.Show();

        //this.Visible = false;
        //frmLogin objLogin = new frmLogin();
        //objLogin.ShowDialog();
    }
    
asked by anonymous 31.01.2015 / 00:03

1 answer

1

There's probably more than one way to do this, I'm going to give you a basic example and you change it according to your needs.

Login Form:

using System;
using System.Windows.Forms;

namespace Aplicativo.Teste
{
    public partial class frmLogin : Form
    {
        // propriedade para indicar se a autenticação foi feita com sucesso.
        public bool Autenticado { get; private set; }

        public frmLogin()
        {
            InitializeComponent();
            Autenticado = false;
        }

        private void btnAutenticar_Click(object sender, EventArgs e)
        {
            // essa verificação é somente para exemplificar
            if (txtSenha.Text == "123456")
            {
                Autenticado = true;
                Close();
            }
            else
            {
                MessageBox.Show("Senha inválida.", "Aviso", MessageBoxButtons.OK, 
                                MessageBoxIcon.Exclamation);
                txtSenha.Focus();
            }
        }
    }
}

Menu Form:

using System;
using System.Windows.Forms;

namespace Aplicativo.Teste
{
    public partial class frmMenu : Form
    {
        // propriedade para indicar se foi feito o logoff.
        public bool Logoff { get; private set; }

        public frmMenu()
        {
            InitializeComponent();
            Logoff = false;
        }

        private void btnLogoff_Click(object sender, EventArgs e)
        {
            Logoff = true;
            Close();
        }
    }
}

Class with the main method:

using System;
using System.Windows.Forms;

namespace Aplicativo.Teste
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            frmLogin fLogin = null;
            frmMenu fMenu = null;
            bool logoff;

            do
            {
                logoff = false;
                fLogin = new frmLogin();

                Application.Run(fLogin);

                if (fLogin.Autenticado)
                {
                    fLogin.Dispose();
                    fLogin = null;

                    fMenu = new frmMenu();

                    Application.Run(fMenu);

                    logoff = (fMenu != null ? fMenu.Logoff : false);

                    fMenu.Dispose();
                    fMenu = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();

            } while (logoff);
        }
    }
}

The code is very simple and easy to understand, if you still have some questions leave in the comments.

    
18.02.2015 / 13:18