C# Confirmation Dialog Box example (ok, cancel)

chris (2004-11-16 12:01:53)
307907 views
10 replies

Somtimes you need to obtain a quick confirmation from a user to confirm an action - say a close, or delete event. This is very simple in Windows Forms UI programming, but is slightly confused by the number of Dialog classes available to the designer. In fact if all you need is a simple confirmation, you will not require a Dialog at all. All you need to do is show a MessageBox with two buttons, 'ok' and 'cancel'.

In this example, a user has clicked on a 'delete' button and I wish to confirm the deletion first:
private void button3_Click(object sender, System.EventArgs e) 
{
   if (MessageBox.Show("Really delete?","Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
   {
       // a 'DialogResult.Yes' value was returned from the MessageBox
       // proceed with your deletion
   }
}

That's it - what could be simpler?

christo

follow me on twitter: http://www.twitter.com/planet_guru

Digg it! Submit to Slashdot Add to Blinklist Del.icio.us Add to Newsvine Add to Technorati Add it to Google Bookmarks
comment
virux
2008-10-15 04:22:04

quite clear

Quite clear.. and short.
thanks
reply icon
marias
2008-11-25 07:13:08

thanq

thanqso much.it helped me a lot and also tooooooo simple
reply icon
Alexei Goncharov
2009-07-17 14:38:23

LOL

You're a genious!!!
reply icon
Sarvesh
2009-08-18 11:55:58

Excellent

Somtimes you need to obtain a quick confirmation from a user to confirm an action - say a close, or delete event. This is very simple in Windows Forms UI programming, but is slightly confused by the number of Dialog classes available to the designer. In fact if all you need is a simple confirmation, you will not require a Dialog at all. All you need to do is show a MessageBox with two buttons, 'ok' and 'cancel'.

In this example, a user has clicked on a 'delete' button and I wish to confirm the deletion first:
private void button3_Click(object sender, System.EventArgs e) 
{
   if (MessageBox.Show("Really delete?","Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
   {
       // a 'DialogResult.Yes' value was returned from the MessageBox
       // proceed with your deletion
   }
}

That's it - what could be simpler?

christo

follow me on twitter: http://www.twitter.com/planet_guru

reply icon
Kashif
2010-01-07 11:58:30

nice

nice n simple !!!
reply icon
silverfall
2010-05-11 04:43:31

Short and sweet

Excellent.
Thanks a lot dude. It's great!
reply icon
khawar
2011-01-07 20:13:54

thanxs

Somtimes you need to obtain a quick confirmation from a user to confirm an action - say a close, or delete event. This is very simple in Windows Forms UI programming, but is slightly confused by the number of Dialog classes available to the designer. In fact if all you need is a simple confirmation, you will not require a Dialog at all. All you need to do is show a MessageBox with two buttons, 'ok' and 'cancel'.

In this example, a user has clicked on a 'delete' button and I wish to confirm the deletion first:
private void button3_Click(object sender, System.EventArgs e) 
{
   if (MessageBox.Show("Really delete?","Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
   {
       // a 'DialogResult.Yes' value was returned from the MessageBox
       // proceed with your deletion
   }
}

That's it - what could be simpler?

christo

follow me on twitter: http://www.twitter.com/planet_guru

reply icon
ayu
2011-01-11 16:17:50

I like it !..

Somtimes you need to obtain a quick confirmation from a user to confirm an action - say a close, or delete event. This is very simple in Windows Forms UI programming, but is slightly confused by the number of Dialog classes available to the designer. In fact if all you need is a simple confirmation, you will not require a Dialog at all. All you need to do is show a MessageBox with two buttons, 'ok' and 'cancel'.

In this example, a user has clicked on a 'delete' button and I wish to confirm the deletion first:
private void button3_Click(object sender, System.EventArgs e) 
{
   if (MessageBox.Show("Really delete?","Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
   {
       // a 'DialogResult.Yes' value was returned from the MessageBox
       // proceed with your deletion
   }
}

That's it - what could be simpler?

christo

follow me on twitter: http://www.twitter.com/planet_guru

reply icon
kishore
2011-05-11 14:46:45


good
reply icon
Annee
2011-06-15 17:39:01

Excellent!

I spent a lot of time googling and checking on the lengthy codes to do this simple thing,
but strong felt that it must be much simpler than that and I knew I was right when I saw your post! Thanks Dude! It's simple,short and Excellent!!!
reply icon