المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : MessageBox is displayed twice.... Help!



C# Programming
04-02-2009, 02:22 PM
Hi Guys,

I am working on a Windows Form where I have placed a Button control (Button text: Exit). In the Button1_Click event Handler, I have added this code:

//Code begins here

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Are you sure to exit the application! Selct Ok to Exit now or Cancel to stay in the current form", "Sample App", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);

if (MessageBox.Show("Are you sure to exit the application! Selct Ok to Exit now or Cancel to stay in the current form", "Sample App", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
Close();
}
else
{
return;
}
}

//Code ends here


However, when I debug the solution and click on the Exit button, the Message Box is displayed asking for confirmation. Then, when I click on the Ok button, instead of closing the application, the same Message Box pops up again! Then, again I have to click on Ok and this time the Click Event executtes and the application closes. Similar is the case with the Cancel Button. Why does the MessageBox display the same message twice? Help me Please....

Your help will be appreciated. Thanx....