Close form without destroying C# visual Studio

Asked

Viewed 252 times

2

I am using a form in my application that needs to be loaded quickly, but when I close this form it always takes time to load, there is a way for me to close the form but it does not reload every time I open?

That way I close the form:

private void pbFechar_Click(object sender, EventArgs e)
    {
        this.Close();
    }
  • 1

    This is not good, you would have to keep it in memory declaring a variable in the main form for example and instead of closing, hiding/ showing the same, imagine doing this with all the Foms of the application ?! There is certainly how to improve the loading of the form, but for this you have to show its code, mainly the constructor and the load event, and the functions that are executed within these

  • Translate and read this: <a href="https://stackoverflow.com/questions/6885041/how-to-load-a-winforms-app-quickly"> Read </a> This will probably help you.

1 answer

4

You can change the behavior of the button close, making it just hide the form, if you use the code below in the Formclosing event of your form, it will only be hidden from the user, but will remain open (consuming resources, of course).

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    if (e.CloseReason == CloseReason.UserClosing) {
        e.Cancel = true;
        Hide();
    }
}

To show again, just use the Show() method in the button event (or other control) that calls the form.

I believe that the ideal solution would be to optimize the loading of the form, to be able to close it normally, consuming less system resources.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.