jae33
jae33

Reputation: 85

logout the main form and show the login form

i know this problem may sound stupid, but sad case, i've been searched online to get solution but still cannot get it correct. My problem now is = Logout button so to exit the main form, then show up the login form again. The code below will NOT show the login form after i click the logout button, it straight away exit the whole application.

void logoutbtn_Click(object sender, EventArgs e)
    {
        CloseSockets(); 
        this.Close();
        serverlogin login = new serverlogin();
        login.Show();   
    }

So, i try to replace this.Hide() instead of this.Close();. But, something even dumb happen. Yes, the login page show after i click the logout button, but when i click the Cancel button at my login form, it didnt exit the whole application where it suppose to exit the whole application. i guess is because the main form is just hiding and not yet close??? Plus, when i try to login again, the login button is also, NOT functioning, cannot login to main page.

i apologize upon my explanation and please tell me if it is very unclear. Please kindly help me. Thank you so much.

Upvotes: 2

Views: 28067

Answers (4)

georges619
georges619

Reputation: 286

I reviewed the first answer, and I found out that there is something wrong. In the code, he closes the form after creating the new thread. I tested it, but it always closed my form. So I switched the this.Close(); with t.Start(); and it worked. Below you have the explanation of the code.

You create a new thread then you close the form you are in (for exemple the menu), and finally you start your new thread. You create a new method where you run your new form. I commented it line by line.

private void btnLogout_Click(object sender, EventArgs e)
{
    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(OpenLoginForm)); //you create a new thread
    this.Close(); //you close your current form (for example a menu)
    t.Start();  //you start the thread
}

public static void OpenLoginForm()
{
    Application.Run(new LoginForm()); //run your new form
}

Upvotes: -1

Ngoc Yen Duong
Ngoc Yen Duong

Reputation: 1

private void btnLogout_Click(object sender, EventArgs e)
    {
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(OpenLoginForm));
        t.Start();
        this.Close();   
    }
    public static void OpenLoginForm()
    {
        Application.Run(new LoginForm());
    }

Upvotes: -2

Ben Voigt
Ben Voigt

Reputation: 283793

You might just want to start a new instance of your application, then exit the old one, when "logout" is selected. This cleans up any resources still in use, and makes it much more difficult to leak data from one user session to the next.

The disadvantage, of course, is that it will be slower, but there's ngen.exe to reduce the cost of restarting the app.

Upvotes: 0

Samich
Samich

Reputation: 30165

You need to define 2 event in your form which will be fired on butttons click and handle it in the main form:

MainForm.cs

void logoutbtn_Click(object sender, EventArgs e)
{
    CloseSockets();
    this.Hide();
    serverlogin login = new serverlogin();

    login.Login += new EventHandler(serverlogin_Login);
    login.Cancel += new EventHandler(serverlogin_Cancel);

    login.Show();
}

private void serverlogin_Login(object sender, EventArgs args)
{
    this.Show();
    // do login
}

private void serverlogin_Cancel(object sender, EventArgs args)
{
    Application.Exit();
    // do exit
}

LoginForm.cs

public event EventHandler Login;
public event EventHandler Cancel;

private void OnLogin()
{
    if (Login != null)
        Login(this, EventArgs.Empty);
}

private void OnCancel()
{
    if (Login != null)
        Login(this, EventArgs.Empty);
}

private void btnLogin_Click(object sender, EventArgs e)
{
    this.OnLogin();
}

private void btnCancel_Click(object sender, EventArgs e)
{
    this.OnCancel();
}

Upvotes: 0

Related Questions