Change of Users

Asked

Viewed 416 times

1

Good evening guys I’m doing a project in college and I came across a problem a while back and I couldn’t solve it. I have the Login screen and within the system I have User Exchange and when I call the Login screen it opens another system on top with what was being used below, when I close the previous application closes everything, I made the authentication close the current screen and open a new one only that when the previous application closes it waits for the confirmation of closure yes or there is something that I can solve it ?

The code below is from the menu button calling the login screen of the user, the commented code is the test I did with an example of the internet more did not work

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();
    }
  • Your question is a bit confusing, try to put an excerpt of code referring to each step you are explaining to facilitate understanding. @Denilson-carlos

  • I believe that my answer helps you to solve your problem, to know a little more of how it works Sopt make a [tour].

1 answer

1

There’s probably more than one way to do this, I’ll 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 method main:

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, in case you still have any doubts leave in the comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.