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();
}
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?
– Edu Mendonça
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?
– Gustavo Pedro
I don’t understand c# but it must have the method
destructor
to release the memory login form... see Docs.microsoft– Edu Mendonça