Question

Before anything I'm really new to C# / Visual Studio / GUIs, so I can't even ask my questions correctly... -What the "x" button of a window/form is called? X Button? -Can I hide it (but not the whole top bar)? -Can I hide only a few of the top buttons, like X and _ ?

I have the following things in my 'testapp'

  • Form1 with button1.
  • Form2 with a label and button2.

Button1, when clicked, checks if theres already a Form2 object (using the Application.OpenForms("Form2") something like that).
If there is, then show it AND hide form1. If there isn't, create 1, show it AND hide form1.

Button2 when clicked, gets a reference to Form1 (using the Application.OpenForms("Form2") something like that), shows it AND hide form2.

But if the user closes the form2 using the "x" my app continues to run (because the mainForm still running, but hidden). I'd like to 'override the x button' to make it show form1, then hide/close the form2.

How can I do that?

Was it helpful?

Solution

What the "x" button of a Window called? X Button?

I normaly call it the "close button", but I don't know if this is (or if there even is a) the official name.

Can I hide it (but not the whole top bar)? Can I hide only a few of the top buttons, like X and _ ?

AFAIK you can hide the minimize and maximize button or all of them, but not the close button alone. You can do that by setting the MaximizeBox and/or MinimizeBox property of the form to false to hide the other two or ControlBox to hide all of them.

But if the user closes the form2 using the "x" my app continues to run (because the mainForm still running, but hidden). I'd like to 'override the x button' to make it show form1, then hide/close the form2

You can register to the Closing event. Inside the closing event you can then show form1 again:

private void Form2_Closing(object sender, CancelEventArgs e) 
{
    e.Cancel = true;
    Hide();

    var form1 = FindForm1(); // here you can insert your logic to get form1
    form1 .Show();
}

OTHER TIPS

You cannot override the "X" directly, but you can handle the Closing event on the window. Then, if you want to hide it, but not close it, and show another form, do the following in the event handler.

private void Form1_Closing(object sender, CancelEventArgs e) 
{
    if (stopClose) 
    {
         e.Cancel = true;
         Hide();

         myOtherForm.Show();
    }  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top