Open Form2 and Close Form1, making Form2 the main (without making it invisible)

Asked

Viewed 143 times

1

Hello, I have the following problem:
I got the forms FrmLogin and FrmSistema. When the user logs in to FrmLogin I open the FrmSitema, lathe the FrmSitema the FormPrincipal, and close the FrmLogin.

NOTE: I don’t want to hide the FrmLogin, as I have seen here many commenting:

this.hide();
FrmLogin.Show();

'Cause think about it, if the user keeps clicking on Log in and on will get full of invisible images using memory atoa, that’s not my goal, someone can help me?

  • You already tried to destroy the form when it closes. So you won’t have to worry about memory... Now... I didn’t see sense in the logged-in user wanting to log back in with the open system?

  • 1

    Hello Edu, Thank you for answering. If he logs in, will display the Frmprincipal, in the main Frm has the option to scroll down and log in with another user. If an employee uses the program and at the end of his turn leaves and dislodges and does not close will open another screen and so on. How to destroy the Form, could give me details and post the code?

  • I don’t understand c# but it must have the method destructor to release the memory login form... see Docs.microsoft

1 answer

0


You should change the way you are starting your application. Consider initializing it with the main form and then call the login form.
After user/password verification, return a positive result, if negative, the main form will be closed.
I used the property Login to write in the main form the username entered in the login frm.

Behold:
Frmprincipal
Function that validates Frmlogin

    /// <summary>
    /// Retorna verdadeira se o login/senha forem válidos
    /// </summary>
    /// <returns></returns>
    public bool LoginValido() {
        //Somente retorna OK se o login/senha forem válidos
        bool loginValido = false;
        FrmLogin frmLogin = new FrmLogin();
        loginValido = frmLogin.ShowDialog(this) == DialogResult.OK;

        Login = frmLogin.Login; //Preenche a propriedade do form principal

        return loginValido;
    }

This is in the load form, will only let Frmprincipal load if the login is valid

    private void FrmPrincipal_Load(object sender, EventArgs e) {
        if (!LoginValido())
            Close();
        lblLogin.Text = Login;
    }

To change users, as you mentioned

    private void BtnTrocarUsuario_Click(object sender, EventArgs e) {
        if (LoginValido()) {
            lblLogin.Text = Login;
            AplicaDiretivas(Login);
        }
    }

No Frmlogin no secret, when validating the user and password, the result of the window should be OK.

    private void BtnLogar_Click(object sender, EventArgs e) {
        //
        // Verificar se login/senha são válidos
        //
        if (login/senha for válido) {
            Login = txtLogin.Text;
            DialogResult = DialogResult.OK;
        }
        else {
            MessageBox.Show("Login/senha inválidos", "Atenção!");
        }
    }

If the user cancels or closes the window, the result will always be negative, this will close the main form

    private void BtnCancelar_Click(object sender, EventArgs e) {
        Close();
    }
  • Thanks, you can do it this way :D

Browser other questions tagged

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