closing second form without closing first form
-
I have two forms. there is a button in the first form, when clicked will open up the second form. Now what i want is that when i click the button on the first form again i want the second form to close. i am aware of the code for opening a second form from the primary.
secondform.show();
but can any one help me in providing with the code for closing this second form??? the second form should close and the first form should stay open.
-
I have two forms. there is a button in the first form, when clicked will open up the second form. Now what i want is that when i click the button on the first form again i want the second form to close. i am aware of the code for opening a second form from the primary.
secondform.show();
but can any one help me in providing with the code for closing this second form??? the second form should close and the first form should stay open.
The simplest possible way to do this is:
private void btnVisibleControl_Click(object sender, EventArgs e)
{
secondForm.Visible = ! secondForm.Visible;
}But, what if the second Form shows a 'CloseBox, and the user closes it ? To guard against that you can define a FormClosing EventHandler in the second Form:
private void secondForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}Note that 'secondForm will be closed when your first Form is closed automatically only if the 'secondForm was created and shown in your first Form's code. Automatic closing of Forms created in the "Main" Form's code, when the Main Form closes, is a standard behavior of the Windows Form programming model. But, there's a better way, where all the code is put in the main Form:
private secondForm f2;
private string strClose = "Close Second Form";
private string strOpen = "Open Second Form";
private void FormTemplate_Load(object sender, EventArgs e)
{
f2 = new secondForm();// by assigning 'secondForm's Closing EventHandler here: // we can synchronize the Button Text easily since the // the EventHandler will have access to the Button f2.FormClosing += f2\_FormClosing; btnVisibleControl.Text = strOpen;
}
// prevent the instance of 'secondForm from actually closing
private void f2_FormClosing(object sender, FormClosingEventArgs e)
{
f2.Hide();// update the Button Text btnVisibleControl.Text = strOpen; e.Cancel = true;
}
// change the Button Text so the end-user sees
// what pressing the Button will do
private void button1_Click(object sender, EventArgs e)
{
if (f2.Visible)
{
btnVisibleControl.Text = strOpen;
}
else
{
btnVisibleControl.Text = strClose;
}// or you can use this to make the code (above) simpler: // btnVisibleControl.Text = (f2.Visible) ? strOpen : strClose; f2.Visible = ! f2.Visible;
}
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
-
The simplest possible way to do this is:
private void btnVisibleControl_Click(object sender, EventArgs e)
{
secondForm.Visible = ! secondForm.Visible;
}But, what if the second Form shows a 'CloseBox, and the user closes it ? To guard against that you can define a FormClosing EventHandler in the second Form:
private void secondForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}Note that 'secondForm will be closed when your first Form is closed automatically only if the 'secondForm was created and shown in your first Form's code. Automatic closing of Forms created in the "Main" Form's code, when the Main Form closes, is a standard behavior of the Windows Form programming model. But, there's a better way, where all the code is put in the main Form:
private secondForm f2;
private string strClose = "Close Second Form";
private string strOpen = "Open Second Form";
private void FormTemplate_Load(object sender, EventArgs e)
{
f2 = new secondForm();// by assigning 'secondForm's Closing EventHandler here: // we can synchronize the Button Text easily since the // the EventHandler will have access to the Button f2.FormClosing += f2\_FormClosing; btnVisibleControl.Text = strOpen;
}
// prevent the instance of 'secondForm from actually closing
private void f2_FormClosing(object sender, FormClosingEventArgs e)
{
f2.Hide();// update the Button Text btnVisibleControl.Text = strOpen; e.Cancel = true;
}
// change the Button Text so the end-user sees
// what pressing the Button will do
private void button1_Click(object sender, EventArgs e)
{
if (f2.Visible)
{
btnVisibleControl.Text = strOpen;
}
else
{
btnVisibleControl.Text = strClose;
}// or you can use this to make the code (above) simpler: // btnVisibleControl.Text = (f2.Visible) ? strOpen : strClose; f2.Visible = ! f2.Visible;
}
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
thanx for the reply...
secondform.close();
this code worked