MessageBoxButton.YesNo Tutorial

I have a little problem, but this time it refers to MessageBoxButton.YesNo . This is my problem: I do not know what the correct syntax is, if I press Enter (keyboard) or click "Yes", Application.Exit(); will be executed Application.Exit(); , and if I press Esc (keyboard) or click "No", Application.Exit(); will not be executed. This is my code:

 MessageBox.Show("Are you sure you want to exit?","Application Exit", MessageBoxButtons.YesNo); Application.Exit(); 
+4
source share
3 answers

You need to actually save and check the result of the message box

 var result = MessageBox.Show("Are you sure you want to exit?", "Application Exit", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { Application.Exit(); } 
+9
source

The MessageBox.Show () function returns an enumeration of DialogResult, and when you specify the Yes / No buttons, you will get one of these results:

 if (MessageBox.Show("Are you sure you want to exit?", "Application Exit", MessageBoxButtons.YesNo) == DialogResult.Yes) { Application.Exit(); } 
+7
source

you need to use DialogResult

 if(MessageBox.Show("....", ..., MessageBoxButtons.YesNo) == DialogResult.Yes){ Apllication.Exit(); } 
+1
source

Source: https://habr.com/ru/post/1485107/


All Articles