I open a form but I can't close it with the same button. How I can do this? I try doing this:
...
{
var openform = new Form2();
if (openform != null) openform.Show();
else openform.Hide();
}
I open a form but I can't close it with the same button. How I can do this? I try doing this:
...
{
var openform = new Form2();
if (openform != null) openform.Show();
else openform.Hide();
}
It seems you have to implement the following logic:
Form2
instance, close itForm2
instance.If it's your case we should look for the opened Form2
instance(s) first; and only then create it with new
(if required)
using System.Linq;
...
// Search: do we have opened Form2 instances?
Form2 openform = Application
.OpenForms // among all opened forms
.OfType<Form2>() // of type Form2
.LastOrDefault(); // in case we have several instances, let's choose the last one
if (null == openform) { // no Form2 instance has been found
openform = new Form2();
openform.Show();
}
else { // Instance (openform) has been found
openform.Close(); // Or openform.Hide();
}
private Form2 form2;
private void button1_Click(object sender, EventArgs e)
{
if (form2 == null || form2.IsDisposed)
{
// Either no form has been created or the last one created has been closed.
form2 = new Form2();
form2.Show();
}
else
{
form2.Close();
}
}
If you like null propagation:
if (form2?.IsDisposed == false)
{
form2.Close();
}
else
{
// Either no form has been created or the last one created has been closed.
form2 = new Form2();
form2.Show();
}
Use bool variable as a switch on button just like following and make openform global.
var openform = new Form2(); // it must be defined out
of the method and within the class as global variable.
private static bool isOpen = true;
Within Button Click method
if (isOpen) openform.Show();
else openform.Hide();
isOpen = !isOpen;
Tested Code
public partial class Form2 : Form
{
public Form2() => InitializeComponent();
Form1 openForm = new Form1();
private static bool isOpen = true;
private void button1_Click(object sender, EventArgs e)
{
if (isOpen) openForm.Show();
else openForm.Hide();
isOpen = !isOpen;
}
}
Check this one tested. https://youtu.be/o9I77dhEvYg
I think it will be more intuitive to the operator if the other form would be closed like all other forms in windows: press a Close button, click the upper right cross, select ALT-F4, etc.
However, if you really want to close the other form from your main-form, you should not just close it, but you should ask the other form nicely if it would be kind enough to close itself. This way you give the other form the possibility to ask the operator some questions, for instance ask if the changed items need to be saved.
private Form myForm = null;
public void OnButton1_Clicked(object sender, ...)
{
if (this.myform == null)
{ // not shown yet. Show it now:
this.myForm = new Form2()
this.myForm. properties = ...
// make sure I get notified if the Form closes in any way:
this.myForm.Closed += onMyFormClosed;
// show the form
this.myform.Show(this);
}
else
{ // ask the form nicely to close itself
this.CloseForm();
// this might (or might not) lead to the event Form.Closed
}
}
private void OnMyFormClosed(object sender, ...)
{
if (!object.ReferenceEquals(sender, this.myForm))
{ // someone else is closed. I have nothing to do with this
return;
}
// if here, my Form is closed. Save to Dispose and assign Null
this.myForm.Dispose();
this.myForm = null;
}
}
public void ShowFo
Maybe something like this will help you:
Form2 openform = new Form2();
if (Application.OpenForms.OfType<Form2>().Count() > 0)
openform.Hide();
openform.Show();
Although I didn't fully understand the context of your question - you might need to adapt the code. You can also check this How to check if a windows form is already open, and close it if it is?.