How to open window form in click 2nd time when it's closed after first time

Posted by Kumartyr under C# on 7/6/2013 | Points: 10 | Views : 1520 | Status : [Member] | Replies : 0
hi guys

How to open window form in click 2nd time after it is closed & it also should not open if it is already opened state

i doing window app using c#.net 2.0 and oracle 10g

i have a form name FORM1 with menustrip

from the menustrip of FORM1, i am opening another form FORM2 and closing the same FORM2 after using it
but if i click that for the second time it is not showing

i also added code that if user press the same menustrip button for the second time before closing the same FORM2 it should not open and bring the already opened FORM2 to focus

below is the code i have tried


First attempt
-----------------

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
FORM2 f2 = Application.OpenForms["FORM2"] as FORM2 ;
if (f2 != null)
{
f2 .WindowState = FormWindowState.Normal;
f2 .BringToFront();
f2 .Activate();
}
else
{
f2 = new FORM2();
f2.Show();
}
}


Second Attempt
--------------


private void FORM2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}


with this code i am able to open same FORM2 multiple times when the user clicks the menustrip multiple times.. i dont want to happen that


third attempt
------------

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{

FORM f2 = new FORM2();
f2.Show();
}

this code also doing the same as second attempt



fourth attempt
--------------

private FORM2 f2 = null;
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2 = new FORM2();
}
f2.Show();
f2.Activate();
}




i have tried all the above coding but am not able to get what i am expecting..

can anyone guide me in this regard




Responses

(No response found.)

Login to post response