简体   繁体   中英

Close WinForm On Button Click

I have this syntax on my buton press event, but when I press it - the form does not close.

What is the proper way to close the form on the button press event?

private void btnClose_Click(object sender, EventArgs e)
{
    IxalocToes nip = new IxalocToes();
    nip.Close();
}

This method btnClose_Click runs inside your forms class

Forms have a method call Close , calling Close() or this.Close() inside the form will close it

private void btnClose_Click(object sender, EventArgs e)
{
    IxalocToes nip = new IxalocToes();
    nip.Close();
    Close();
}

As suggested by many and it is right, calling

this.Close() or
Close()

will close the form. As you want to know why nip.Close() is not working, it's because the button is in a FORM but when you call nip.Close() instead of this.Close(), it will close the new object created, not the one on which the button resides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM