2017-05-13 1 views
-2

내 코드에서 타이머를 사용 중이며 타이머가 0에서 멈 추면 messagebox가 시간 초과되었음을 알리고 두 개의 버튼 "재시도"및 "취소"를 표시합니다. 메시지 상자에서 "CANCEL (취소)"버튼을 누르면 전체 윈도우 양식이 종료됩니다.Messagebox 버튼 액세스 C#

int duration = 10; 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     //shows message that time is up! 
     duration--; 
     timer_label1.Text = duration.ToString(); 
     if (duration == 0) 
     { 
      timer1.Stop(); 
      MessageBox.Show("You Timed Out", "Oops", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop); 

     } 
    } 

    private void start_game_button19_Click(object sender, EventArgs e) 
    { 
     timer1.Enabled = true; 
     timer1.Start(); 
    } 

enter image description here

+0

지금까지 시도 무엇을 게시하시기 바랍니다, 그래서 우리는 .. 당신을 도울 수 –

+0

INT 시간 = 10; private void timer1_Tick (객체 발신자, EventArgs e) { // 시간이 다되었다는 메시지를 보여줍니다! 기간 -; timer_label1.Text = duration.ToString(); if (duration == 0) { timer1.Stop(); MessageBox.Show ("You Timed Out", "죄송합니다", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop); }} 개인 무효 start_game_button19_Click (객체 송신자있는 EventArgs E) { timer1.Enabled = TRUE; timer1.Start(); } – AmberYaseen

+1

원래 게시물을 수정할 수 있습니다. 댓글 영역에 긴 코드가 도움이되지 않습니다. – Anthony

답변

0

하나는 변수에 Show 호출의 결과를 할당해야합니다, MessageBox 작동하고 버튼을 클릭에 따라 다른 조치를 취할 수 : 아래 timer_tick 이벤트의 경우 조건 . Show은 클릭 한 버튼을 결정하는 데 사용할 수있는 DialogResult 값을 반환합니다.

var retryOrCancel = MessageBox.Show(
    text: "You Timed Out", 
    caption: "Oops", 
    buttons: MessageBoxButtons.RetryCancel, 
    icon: MessageBoxIcon.Stop 
); 

switch (retryOrCancel) 
{ 
    case DialogResult.Cancel: 
    this.Close(); 
    break; 
    case DialogResult.Retry: 
    StartGame(); 
    break; 
} 

private void start_game_button19_Click(object sender, EventArgs e) 
{ 
    StartGame(); 
} 

private void StartGame() 
{ 
    timer1.Enabled = true; 
    timer1.Start(); 
} 
+0

이 문제가 해결되었습니다. 감사합니다. :) – AmberYaseen

+0

도와 드리겠습니다. 당신이 그것이 잘 설명한다고 생각한다면이 대답에 투표하십시오. –

0

당신은 코드를 다음과 같은 것을 할 수 있습니다

var result = MessageBox.Show(
       "You Timed Out", 
       "Oops", 
       MessageBoxButtons.RetryCancel, 
       MessageBoxIcon.Stop); 

if (result == DialogResult.Cancel) { 
    this.Close(); 
}