2011-03-24 46 views

cevap

55
  1. Çağrı Evet/No düğmelerini yerine Tamam düğmesini almak için MessageBoxButtons.YesNo geçmek.

  2. aslında Evet ve Hayır düğmeleri istiyorsanız

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

@xxMUROxx Lütfen refactor koduyla ilgili yanıtları düzenlemeyin. İyileştirmelerle ek içerik eklemeniz gerektiğini düşünüyorsanız, yorum bırakın veya kendi yanıtınızı ekleyin. –

6
if(DialogResult.OK==MessageBox.Show("Do you Agree with me???")) 
{ 
     //do stuff if yess 
} 
else 
{ 
     //do stuff if No 
} 
+1

Çok teşekkür ederim efendim :) – 6TTW014

+0

@ Kimmy25: Kullandıysanız lütfen cevabı kabul edin. – Lazlo

+3

Bu, özellikle tercih edilmeyecek, çünkü başka bir yerde bulunan cümleler herhangi bir zamanda çağrılmayacak. –

8

.... DialogResult.Yes için (iletişim dönene kadar yürütme engeller) Bu aramanın sonucunu karşılaştırın (ve varsayarak 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; 
    } 
} 
0

Kontrol bu:

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

Selamlar

2

.NET 4.5 için doğru cevabın güncellenmiş sürümü olacaktır. Eğer bir görünüm modelinde bir komuta düğmeye bağlamak istiyorsa

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

Ayrıca, aşağıdakileri kullanabilirsiniz. Bu MvvmLite uyumludur:

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

MessageBox penceresinde 'EVET' veya 'hayır' düğmelerini basarken durumunu kontrol etmek Bu şekilde.

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