문제

I am trying to make it so that the user is prompted to confirm exiting my program in c#, but for some reason, if they say "yes" they would like to exit, the confirmation box would pop up again. I can't figure out why.

    if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
    {
        e.Cancel = true;
    }
    else { Application.Exit(); }
도움이 되었습니까?

해결책

Ah, did you check the CloseReason for the FormClosing event? I think you might get the same event for two different reasons (although I don't exactly expect that to happen normally); check your FormClosingEventArgs to see what the parameters are.

다른 팁

Use this

 private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Are you sure you want to close?", "Infomate", MessageBoxButtons.YesNo) == DialogResult.No)
        {
            e.Cancel = true;
        }        
    }

Ah, I figured out how to fix it. I removed the Application.Exit(); event from the FormClosing event, and moved it into the FormClosed event. It all works now.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top