Habili Menus by MDI Parent

0

Good afternoon everyone, I have the following existential doubt, I have a WindowsForms MDI Parent form , in which I show a series of windows, but in the event Load I need to upload a Login form , which, by pressing the Validate button, if the user is correctly authenticated, enable the menus , close the < strong> form of the Login and can run the different applications, the code I am using is the following:

public void MuestraLogin()
{
    frmLogin objLogin = new frmLogin();
    objLogin.MdiParent = this;
    objLogin.Show();
}
public void InhabilitaMenus()
{
    try
    {
        menuStrip1.Enabled = false;
        menuStrip1.Visible = false;
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}
public void ObtenerValidacion(bool valido)
{
    try
    {
        if (Valido)
        {
            menuStrip1.Visible = true;
            menuStrip1.Enabled = true;
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}
private void frmMenu_Load(object sender, EventArgs e)
{
    MuestraLogin();
    InhabilitaMenus();           
}
    
asked by Alberto Arenas 02.12.2017 в 00:33
source

2 answers

1
  

Generally, logging is considered before allowing the main screens to be displayed, for example: container, menus, etc.

Considering your case, you could modify:

frmLogin:

// Creando variable del formulario que contiene el menú
readonly frmMenu frmPrincipal;

// Modificar constructor para referenciar al formulario que contiene el menú
public frmLogin(frmMenu principal)
{
    InitializeComponent();

    frmPrincipal = principal;
}

// Evento click de tu botón que valida
private void btnValidad_Click(object sender, EventArgs e)
{
    frmPrincipal.ObtenerValidacion(true);
    this.Close();
}

frmMenu:

// Pasar por parámetro el formulario que contiene el menú
public void MuestraLogin()
{
    frmLogin objLogin = new frmLogin(this);
    objLogin.MdiParent = this;
    objLogin.Show();
}

Where, frmPrincipal is the class name that contains the menus.

    
answered by 02.12.2017 / 03:19
source
1

I would not enter logic that has to do with the main form in the login form. If it is a login form, the internal logic should be exclusively for that: to validate the user.

What I would do is show the login form as a dialog and it will return a value indicating whether the validation has been successful or not.

The code of the accept button on the login form could be something like this:

private void btnAceptar_Click(object sender, EventArgs e)
{
    DialogResult = ValidarUsuario()
        ? DialogResult.Yes
        : DialogResult.No;
    Close();
}

If the user validation is correct, the form returns DialogResult.Yes , otherwise returns DialogResult.No .

In the main MDI form you could open this dialog as:

private void frmMenu_Load(object sender, EventArgs e)
{
    InhabilitarMenus();
    var login = new frmLogin();
    var usuarioValido = login.ShowDialog() == DialogResult.Yes;
    ObtenerValidacion(usuarioValido);
}

After disabling the menus, the login form with ShowDialog is displayed and the user is considered valid if this form returns a DialogResult.Yes result. With this information you could already call the ObtenerValidacion method.

    
answered by 02.12.2017 в 23:05