2010-11-30 4 views
-1

Head First C#을 읽는 동안이 작은 프로그램을 만들었습니다. 난 어려움의 수준에 대한 numericUpDown 컨트롤을 설정하는 사용자를 위해 프로그램을 일시 중지하려고 노력하고있어. 코드는 다음과 같습니다.C#에서 사용자 컨트롤을 일시 중지하는 방법은 무엇입니까?

namespace WACK 
{ 
    public partial class Form1 : Form 
    { 
     public int level; 
     Mole mole; 
     Random random = new Random(); 

     public Form1() 
     { 
      InitializeComponent(); 

      mole = new Mole(random, new Mole.PopUp(MoleCallBack)); 
      MessageBox.Show("Select a Level"); 
      if (level == 1) 
      { 
       timer1.Interval = random.Next(500, 500); 
      } 
      if (level == 2) 
      { 
       timer1.Interval = random.Next(400, 400); 
      } 
      if (level == 3) 
      { 
       timer1.Interval = random.Next(300, 300); 
      } 
      timer1.Start(); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      timer1.Stop(); 
      ToggleMole(); 
     } 

     private void ToggleMole() 
     { 
      if (mole.Hidden == true) 
       mole.Show(); 
      else 
       mole.HideAgain(); 
      if (level == 1) 
      { 
       timer1.Interval = random.Next(500, 500); 
      } 
      if (level == 2) 
      { 
       timer1.Interval = random.Next(400, 400); 
      } 
      if (level == 3) 
      { 
       timer1.Interval = random.Next(300, 300); 
      } 
      timer1.Start(); 

     } 

     private void MoleCallBack(int moleNumber, bool show) 
     { 
      if (moleNumber < 0) 
      { 
       timer1.Stop(); 
       return; 
      } 
      Button button; 
      switch (moleNumber) 
      { 
       case 0: button = button1; break; 
       case 1: button = button2; break; 
       case 2: button = button3; break; 
       case 3: button = button4; break; 
       case 4: button = button5; break; 
       case 5: button = button6; break; 
       case 6: button = button7; break; 
       case 7: button = button8; break; 
       default: button = button9; break; 
      } 

      if (show == true) 
      { 
       button.Text = "Hit Me!"; 
       button.BackColor = Color.Red; 
      } 
      else 
      { 
       button.Text = ""; 
       button.BackColor = SystemColors.Control; 
      } 

      timer1.Interval = random.Next(500, 1000); 
      timer1.Start(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      mole.Smacked(0); 
     } 

     private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
     { 
      level = (int)numericUpDown1.Value; 
     } 

나머지 버튼 핸들러는 게시물을 더 짧게 만들기 위해 생략되었습니다. 또한 폼이 내 pause 문을 가져야하는 곳이라고 생각하기 때문에 두더지 클래스를 게시하지 않았습니다. 물론 내가 틀릴 수도 있습니다.

답변

1

Windows 응용 프로그램은 사용자 입력을 기다리는 동안 일시 중지되도록 설계되지 않았습니다. 이는 콘솔 응용 패러다임입니다.

이 문제를 해결하는 더 좋은 방법은 난이도가 설정 될 때까지 다른 컨트롤을 사용하지 않도록 설정하거나 숨기는 것입니다. 처음에는 난이도 NumericUpDown과 시작 버튼 만 표시합니다. 시작 버튼을 누르면 나머지 컨트롤을 활성화/표시하고 시작합니다.

+0

확실히 돌아가서 컨트롤을 사용하지 않는 것이 좋지만 일시 중지는 내가 갇혀있는 것입니다. 그래서 토글 첩자()를 호출하기위한 시작 버튼을 통합했다면, 이와 같은 것이 어떻게 될 것인지 생각하십니까? 이제 나는 그것에 대해 생각하고, 나는 대답 할 수있다. 시도해 보라. 그것이 효과가 없다면, 다른 것을 생각해 보라. hehe –

+0

어쩌면 방금 이것을 놓쳤습니다. 확실하지 않다. 하지만 메시지 상자를 사용하여 사용자 입력을받을 수 있음을 발견했습니다. 워라! 사용자 입력을 위해 일시 ​​중지하는 앱이기 때문에 나는 매일 무언가를 배우고 있습니다. –

+0

예, 입력을위한 모달 대화 상자를 표시하는 것이 다른 방법입니다. –