-2

In VB.NET, you can freely Hide/Show/ShowDialog (for a fresh form). But in C#, you always have to create new form instance to do this. Even your previosly hidden form can't show back, you have to create new instance again. Resulting in your previously hidden form will run as background process until you kill it.

My question is how can I create a function that will unhide a form that doesn't need a new instance when I close the current form using the default exit in form's title bar.

Edit: I solved it on my own. It's just confusing. And what difference between c# and vb.net is that I never have to use form_closed in vb.net. Why do I always see it in c#.

Christian Vibora
  • 103
  • 1
  • 11
  • Maybe what you mean is previously *closed* form cannot be shown back? Check on `Form.Show()` and `Form.Hide()` – Ian Jan 18 '16 at 09:58
  • 1
    C# and VB.NET both need the reference to the instance of the form. – Camilo Terevinto Jan 18 '16 at 09:58
  • It seems that you discard the variable that keeps the reference to the form instance created. If you keep available that instance you can re-show the hidden form – Steve Jan 18 '16 at 09:58
  • Your theory about C# is highly inaccurate. Check [this post](http://stackoverflow.com/a/4699360/17034) to get the vb.net default instance feature back. – Hans Passant Jan 18 '16 at 10:11
  • Yeah yeah I know. But on what I've built up, it always needs the form_closed event. – Christian Vibora Jan 18 '16 at 10:21

1 Answers1

2

You can always display an hidden form in C#. There are many ways to this.
For example you could check the Application.OpenForms collection that keeps track of all forms owned by your application and still not closed. (An hidden form is not closed). This approach means that you need to identify you form between the ones included in the collection OpenForms

MyFormClass f = Application.OpenForms.OfType<MyFormClass>().FirstOrDefault();
if ( f != null) f.Show();

Another approach, more lengthy, is to keep track yourself of the form to hide/show with a variable inside your application. This requires extra care in handling correctly the various situations in which you global variable becomes invalid (what if the user closes effectively the form instead of hiding it?)

This is an example that you can test easily with LinqPAD

// Your form instance to hide/show
Form hidingForm = null;

void Main()
{
    Form f = new Form();
    Button b1 = new Button();
    Button b2 = new Button();
    b1.Click += onClickB1;
    b2.Click += onClickB2;

    b1.Location = new System.Drawing.Point(0,0);
    b2.Location = new System.Drawing.Point(0, 30);
    b1.Text = "Hide";
    b2.Text = "Show";

    f.Controls.AddRange(new Control[] {b1, b2});
    f.ShowDialog();
}

void onClickB1(object sender, EventArgs e)
{
    // Hide the global instance if it exists and it is visible
    if(hidingForm != null && hidingForm.Visible)
        hidingForm.Hide();
}

void onClickB2(object sender, EventArgs e)
{
    // Create and show the global instance if it doesn't exist
    if (hidingForm == null)
    {
        hidingForm = new Form();
        hidingForm.Show();

        // Get informed here if the user closes the form
        hidingForm.FormClosed += onClosed;
    }
    else
    {
        // Show the form if it is not visible
        if(!hidingForm.Visible)
            hidingForm.Show();
    }
}
void onClosed(object sender, FormClosedEventArgs e)
{
    // Uh-oh.... the user has closed the form, don't get fooled...
    hidingForm = null;
}

Well, OpenForms seems a lot better.

Steve
  • 213,761
  • 22
  • 232
  • 286