Alucsard
Alucsard

Reputation: 33

Prevent the Form from closing when ERROR occures

Using winforms, when error occures, DialogResult will be Ok and the form will close, Is there a way for the form not to close ? I need DialogResult = DialogResult.OK; that part to be in my code too.

private void btnSave_Click(object sender, EventArgs e)
{
    if(NotERROR)
    {
        doSomething;
    } 
    else 
    {
        MessageBox.Show("ERROR");
    }
    DialogResult = DialogResult.OK;
}

Upvotes: 0

Views: 57

Answers (1)

ASh
ASh

Reputation: 35646

set DialogResult.OK only when there is no error, inside if block:

private void btnSave_Click(object sender, EventArgs e)
{
    if (NotERROR)
    {
        DoSomething();
        DialogResult = DialogResult.OK;
    } 
    else 
    {
        MessageBox.Show("ERROR");
    }
}

Upvotes: 3

Related Questions