Search code examples
c#winformsmessagebox

Form is closing on message box button click


I am facing a strange problem, I am showing a message box with yes|no buttons, but whenever any of the button on message box is clicked it closes the form also.

I thought it is something related to my code so I tried only this much code on my button click

if(MessageBox.Show("Are you sure you want to delete this category?", "Delete Category",
                    MessageBoxButtons.YesNo, 
                    MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) 
                    == DialogResult.Yes)
 {
     // doing nothing just to check
 }

But that is also closing the form.

I tried putting button in different places, tried adding different buttons etc but nothing is working.

Is there anything I can try or could be the reason?


Solution

  • Check DialogResult property of the button you use to show the message dialog. It must be set to DialogResult.None.

    And as VahidND suggested in his comment, ensure that the button is not set as its parent form AcceptButton or CancelButton.