hi there,

i have two forms in my project, what i want is when i cilck the close icon on top of the form the login form should be displayed, how can i do this in C#??

Add "FormClosing" event to your form. And use this code:

private void Form_Closing(object sender, FormClosingEventArgs e)
{
    // Remove these 2 lines if the current form is not the default form of your application.
    e.Cancel = true;
    this.Hide();
    
    LoginForm loginForm = new LoginForm();
    loginForm.Show();
}

Thanks

Edited 6 Years Ago by farooqaaa: n/a

Add "FormClosing" event to your form. And use this code:

private void Form_Closing(object sender, FormClosingEventArgs e)
{
    // Remove these 2 lines if the current form is not the default form of your application.
    e.Cancel = true;
    this.Hide();
    
    LoginForm loginForm = new LoginForm();
    loginForm.Show();
}

Thanks

it dosen't work it gives an stack overflow exception???

why is that???????????


thanxxxxx

I've tested the code and its working perfectly. There might be something wrong with your code. Can you post your code?

Okay. You are making a mistake there. When you call this.Close() FormClosing event is fired. But inside FormClosing event you've called this.Close() again which causes a stackoverflow.

Here's the working code:

private void btnPClose_Click(object sender, EventArgs e)
{
    this.Close();
}

private void frmProposalInProgress_FormClosing(object sender, FormClosingEventArgs e)
{
   new frmLogin().Show();
}

Thanks

Okay. You are making a mistake there. When you call this.Close() FormClosing event is fired. But inside FormClosing event you've called this.Close() again which causes a stackoverflow.

Here's the working code:

private void btnPClose_Click(object sender, EventArgs e)
{
    this.Close();
}

private void frmProposalInProgress_FormClosing(object sender, FormClosingEventArgs e)
{
   new frmLogin().Show();
}

Thanks

hey it dosen't work properly

the thing is when i login and go to the second form the login form displays???

This article has been dead for over six months. Start a new discussion instead.