How to prevent or block closing a WinForms window?

后端 未结 8 615
情书的邮戳
情书的邮戳 2020-12-01 04:01

How can I prevent window closing by showing a MessageBox? (Technology:WinForms with C#)

When the close event occurs I want the

相关标签:
8条回答
  • 2020-12-01 04:56
    private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
        var window = MessageBox.Show(
            "Close the window?", 
            "Are you sure?", 
            MessageBoxButtons.YesNo);
    
        e.Cancel = (window == DialogResult.No);
    }
    
    0 讨论(0)
  • 2020-12-01 04:56

    For prevent or block the form closing in particular situation you can use this strategy:

    private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
    {
    
        if (FLAG_CONDITION == true)
        {
            MessageBox.Show("To exit save the change!!");
            e.Cancel = true;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题