alhadwi alhadwi - 2 months ago 6
C# Question

How to open my "logged in" main form, by clicking a button from another Form? and without login again

public partial class Menu : Form
{

// this is my main form, What I really want
//is not re-enter my name and password
//when I call this form from another form button.
public Menu()
{

InitializeComponent();

{

}
unidadesToolStripMenuItem.Enabled = false;
materialesToolStripMenuItem.Enabled = false;
datosToolStripMenuItem.Enabled = false;
chequeoToolStripMenuItem.Enabled = false;
informeToolStripMenuItem.Enabled = false;
proyectoToolStripMenuItem.Enabled = false;
}

// public void MenuDatosComb();
// if()

public bool UsuarioLogueado = false;
private void Menu_Load(object sender, EventArgs e)
{

this.Hide();
Login login = new Login();
login.ShowDialog();
if (login.Logueado == true)
{
this.Show();
UsuarioLogueado = true;
toolStripStatusLabel2.Text = "Usuario: " + login.Resultado.Substring(20);
}
else
{
this.Close();
}
}

Answer

Try this approach, not the best but will work for you.

In the Login Form, add a static property like this

public static bool Logueado { get; private set; }

Then in the Login Form, when the user connects successfuly, you set Logueado = true;

Now in your Main Form, in the load event, you just do this :

this.Hide();
if (Login.Logueado == true)
{
    // show
}
else
    // close

With this, you'll not need to create a new Login form in the load event, thus, you'll not get another login form.