2013-05-02 19 views
1

winform 응용 프로그램이 있습니다. 모든 필드에 입력 할 때 저장 버튼이 있습니다. 저장 버튼을 클릭하면 messagebox에 저장된 레코드가 성공적으로 나타납니다. 메시지 상자에는 '예'와 '아니요'버튼이 있습니다. 예이면 레코드를 저장하고 양식의 모든 필드를 지워야하며, 아니오를 클릭하면 레코드를 저장하지 않고 양식에서 모든 필드를 지워야합니다. 당신은이에 대한 DialogResult Enumeration을 사용할 수 있습니다메시지 상자 단추의 이벤트 처리기

DialogResult result = MessageBox.Show("text", "caption", MessageBoxButtons.YesNo); 
if(result == DialogResult.Yes){ 
    //yes... 
} 
else if(result == DialogResult.No){ 
    //no... 
} 
+4

Stackoverflow는 무료 코드 작성 서비스는 아닙니다. 시도한 것을 보여주십시오. –

+2

대단하네, 뭐가 문제 야? –

+6

질문을 잊어 버렸습니다. – I4V

답변

15

메시지 박스 클래스의 표시 방법은 DialogResult를을 반환있다.

if(MessageBox.Show("Title","Message text",MessageBoxButtons.YesNo) == DialogResult.Yes) 
{ 
//do something 
} 
2

은 (MSDN에서) 그런 일을 처리 할 수 ​​DialogResult -enum

private void validateUserEntry5() 
{ 
    // Checks the value of the text. 
    if(serverName.Text.Length == 0) 
    { 
     // Initializes the variables to pass to the MessageBox.Show method. 
     string message = "You did not enter a server name. Cancel this operation?"; 
     string caption = "No Server Name Specified"; 
     MessageBoxButtons buttons = MessageBoxButtons.YesNo; 
     DialogResult result; 
     // Displays the MessageBox. 
     result = MessageBox.Show(this, message, caption, buttons); 
     if(result == DialogResult.Yes) 
     { 
      // Closes the parent form. 
      this.Close(); 
     } 
    } 
}