Override standard close (X) button in a Windows Form

后端 未结 10 1577
独厮守ぢ
独厮守ぢ 2020-12-04 08:43

How do I go about changing what happens when a user clicks the close (red X) button in a Windows Forms application (in C#)?

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 08:53

    One thing these answers lack, and which newbies are probably looking for, is that while it's nice to have an event:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        // do something
    }
    

    It's not going to do anything at all unless you register the event. Put this in the class constructor:

    this.FormClosing += Form1_FormClosing;
    

提交回复
热议问题