Hide form instead of closing when close button clicked

前端 未结 7 1868
南旧
南旧 2020-11-28 08:47

When a user clicks the X button on a form, how can I hide it instead of closing it?

I have tried this.hide() in FormClosing but

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 09:12

    If you want to use the show/hide method I've actually done this myself for a menu structure a game I've recently done... This is how I did it:

    Create yourself a button and for what you'd like to do, for example a 'Next' button and match the following code to your program. For a next button in this example the code would be:

    btnNext.Enabled = true; //This enabled the button obviously
    this.Hide(); //Here is where the hiding of the form will happen when the button is clicked
    Form newForm = new newForm(); //This creates a new instance object for the new form
    CurrentForm.Hide(); //This hides the current form where you placed the button.
    

    Here is a snippet of the code I used in my game to help you understand what I'm trying to explain:

        private void btnInst_Click(object sender, EventArgs e) 
        {
            btnInst.Enabled = true; //Enables the button to work
            this.Hide(); // Hides the current form
            Form Instructions = new Instructions(); //Instantiates a new instance form object 
            Instructions.Show(); //Shows the instance form created above
        }
    

    So there is a show/hide method few lines of code, rather than doing a massive piece of code for such a simple task. I hope this helps to solve your problem.

提交回复
热议问题