-6

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();
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • 10
    if `openform` is `null`, how would you call `hide` on it? – Rotem Jul 23 '18 at 09:48
  • 5
    make `openform` a member instead of a local variable – Thomas Weller Jul 23 '18 at 09:48
  • Just take a global variable, say isOpen and set it while opening and check for it before closing. isOpen = false; { Form2 openform = new Form2(); if (!isOpen&&openForm!=null) { openform.Show(); } else { if(openForm!=null) openform.Hide(); } } – Anil Jul 24 '18 at 11:29

5 Answers5

1

It seems you have to implement the following logic:

  1. If there's an opened Form2 instance, close it
  2. Otherwise create and show the new Form2 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();   
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0
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();
}
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

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

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Arslan Ali
  • 450
  • 4
  • 12
  • Why do you need isOpen variable? If only you open documentation of [Show](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.show(v=vs.110).aspx) or [Hide](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.hide(v=vs.110).aspx) and read the Remarks... – Renatas M. Jul 23 '18 at 10:02
  • @Reniuz it's working like a bit when it is checked to be true then Hide else Show the form having same instant of openForm which instantiated once time on constructor. – Arslan Ali Jul 23 '18 at 10:13
  • You didn't get it. Open the documentation and read the Remarks section. In section you will find that when you call `Show` or `Hide`, the `Visible` property is toggled. So you don't need `isOpen` variable - you can use `openform.Visible`. AND if you read remarks **very closely** you will find that calling those methods is equivalent to setting Visible property. So you code can be minimized to one-liner: `openform.Visible != openform.Visible`. – Renatas M. Jul 23 '18 at 10:22
  • you can assign openform.visible to isOpen for first time check it can also be used with isOpen variable thanks for more clearing. – Arslan Ali Jul 23 '18 at 10:39
  • `isOpen` isn't a global static if it's private because no one can see it besides the class – AustinWBryan Jul 28 '18 at 13:01
0

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
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
-1

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

Afonso
  • 323
  • 4
  • 14