MessageBox按钮?

如果按下消息框上的“是”按钮,我该怎么说呢?那个和那个? 在C#中。

  1. 您对MessageBox.Show调用需要传递MessageBoxButtons.YesNo以获取Yes / No按钮而不是OK按钮。

  2. 比较该调用的结果(将阻止执行直到对话框返回)到DialogResult.Yes ….

 if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { // user clicked yes } else { // user clicked no } 

如果你真的想要Yes和No按钮(并假设WinForms):

 void button_Click(object sender, EventArgs e) { var message = "Yes or No?"; var title = "Hey!"; var result = MessageBox.Show( message, // the message to show title, // the title for the dialog box MessageBoxButtons.YesNo, // show two buttons: Yes and No MessageBoxIcon.Question); // show a question mark icon // the following can be handled as if/else statements as well switch (result) { case DialogResult.Yes: // Yes button pressed MessageBox.Show("You pressed Yes!"); break; case DialogResult.No: // No button pressed MessageBox.Show("You pressed No!"); break; default: // Neither Yes nor No pressed (just in case) MessageBox.Show("What did you press?"); break; } } 
 if(DialogResult.OK==MessageBox.Show("Do you Agree with me???")) { //do stuff if yess } else { //do stuff if No } 

.NET 4.5的正确答案的更新版本将是。

 if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxImage.Question) == MessageBoxResult.Yes) { // If yes } else { // If no } 

此外,如果要将按钮绑定到视图模型中的命令,可以使用以下命令。 这与MvvmLite兼容:

 public RelayCommand ShowPopUpCommand { get { return _showPopUpCommand ?? (_showPopUpCommand = new RelayCommand( () => { // Put if statement here } })); } } 

检查一下:

  if ( MessageBox.Show(@"Are you Alright?", @"My Message Box",MessageBoxButtons.YesNo) == DialogResult.Yes) { //YES ---> Ok IM ALRIGHHT } else { //NO --->NO IM STUCK } 

问候

这种方式在MessageBox窗口中按“YES”或“NO”按钮的同时检查条件。

 DialogResult d = MessageBox.Show("Are you sure ?", "Remove Panel", MessageBoxButtons.YesNo); if (d == DialogResult.Yes) { //Contents } else if (d == DialogResult.No) { //Contents }