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#??

Recommended Answers

All 6 Replies

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

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?

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

i have attached a document with this,

thanxxx

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???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.