2012-01-08 3 views
0

확인 버튼과 관련하여 도움이 필요합니다. 사용자가 텍스트 상자에있는 42 개의 숫자를 모두 추가하고 "Enter number"영역에 0-9의 숫자를 입력 한 다음 시작 버튼을 클릭하면 빨간색 레이블이 붙은 레이블 배열 또는 lblCovece와 같은 숫자를 입력해야합니다. 그리고 체크 버튼을 클릭 한 후, 프로그램은 적색 레이블로 선택된 값이 입력 된 숫자와 동일한 지 먼저 확인해야합니다. 유효하면 레이블이 녹색으로 바뀌고 lblResultE 레이블에 결과가 나타납니다 (예 : 결과는 다음과 같아야합니다 : 입력 된 숫자가 2이고 결과가 2 + 2 + 2 ... 인 경우). if lblResultE에서 유효하지 않습니다. 우리는 10 점을 뽑습니다. 그게 내가 지금 도와 준 일이야. :) 고마워.유효성 검사 및 동작이 포함 된 버튼 이벤트

namespace Seminarska 
    { 
public partial class Form1 : Form 
    { 
    private Label l,l2,lblCovece,l4,lblResultE; 
    private Button bUp, bRight, bLeft, bDown, bCheck,bStart, bReset; 
    private TextBox txtVnes, txtGoal; 
    private Label[] pole; 

    public Form1() 
    { 
     InitializeComponent(); 
     l2 = new Label(); 
     l2.Text = " Enter one number"; 
     l2.Location = new Point(230, 200); 
     l2.AutoSize = true; 
     l4 = new Label(); 
     l4.Text = "Score"; 
     l4.Location = new Point(240, 260); 
     l4.AutoSize = true; 
     lblResultE = new Label(); 
     lblResultE.Location = new Point(350, 260); 
     lblResultE.AutoSize = true; 
     bLeft = new Button(); 
     bLeft.Location = new Point(0, 250); 
     bLeft.Width=75; 
     bLeft.Height = 25; 
     bLeft.Text = "LEFT"; 
     bCheck = new Button(); 
     bCheck.Location = new Point(75, 250); 
     bCheck.Width = 75; 
     bCheck.Height = 25; 
     bCheck.Text = "Check"; 
     bRight = new Button(); 
     bRight.Location = new Point(150, 250); 
     bRight.Width = 75; 
     bRight.Height = 25; 
     bRight.Text = "RIGHT"; 
     bUp = new Button(); 
     bUp.Location = new Point(75, 220); 
     bUp.Width = 75; 
     bUp.Height = 25; 
     bUp.Text = "UP"; 
     bDown = new Button(); 
     bDown.Location = new Point(75, 280); 
     bDown.Width = 75; 
     bDown.Height = 25; 
     bDown.Text = "DOWN"; 
     bStart = new Button(); 
     bStart.Location = new Point(240, 165); 
     bStart.Width = 75; 
     bStart.Height = 25; 
     bStart.Text = "START"; 
     bReset = new Button(); 
     bReset.Location = new Point(320, 165); 
     bReset.Width = 75; 
     bReset.Height = 25; 
     bReset.Text = "RESET"; 
     txtVnes = new TextBox(); 
     txtVnes.Location = new Point(240, 10); 
     txtVnes.Width = 160; 
     txtVnes.Height = 130; 
     txtVnes.Multiline = true; 
     txtGoal = new TextBox(); 
     txtGoal.Width = 75; 
     txtGoal.Height = 25; 
     txtGoal.Location = new Point(330, 200); 
     lblCovece = new Label(); 
     lblCovece.Location = new Point(160,165); 
     lblCovece.Width = 20; 
     lblCovece.Height = 20; 
     lblCovece.TextAlign = ContentAlignment.MiddleCenter; 
     lblCovece.Text = "O"; 
     lblCovece.BackColor = Color.FromArgb(255, 0, 0); 
     int a = 0; 
     pole = new Label[42]; 
     this.Controls.Add(lblCovece); 
     for (int i = 1; i <= 6; i++) 
     { 
      for (int j = 1; j <= 7; j++) 
      {      
       l = new Label(); 
       l.Name = "label" + i.ToString() + j.ToString();      
       l.Text = "Z"; 
       l.Width = 20; 
       l.Height = 20; 
       l.TextAlign = ContentAlignment.MiddleCenter; 
       l.Parent = this;      
       l.BackColor = Color.FromArgb(100, 149, 237);      
       l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25); 
       pole[a] = l;     
       this.Controls.Add(l);      
       a++; 
      } 
     } 

     this.Width = 460; 
     this.Height = 380;   
     this.Controls.Add(l2);  
     this.Controls.Add(l4); 
     this.Controls.Add(lblResultE); 
     this.Controls.Add(lblTimeE);    
     this.Controls.Add(bStart); 
     this.Controls.Add(bReset); 
     this.Controls.Add(txtGoal); 
     this.Controls.Add(txtVnes); 
     this.Controls.Add(bUp); 
     this.Controls.Add(bLeft); 
     this.Controls.Add(bRight); 
     this.Controls.Add(bDown); 
     this.Controls.Add(bCheck); 

     bStart.Click+=new EventHandler(bStart_Click); 
     bUp.Click+=new EventHandler(bUp_Click); 
     bDown.Click+=new EventHandler(bDown_Click); 
     bLeft.Click+=new EventHandler(bLeft_Click); 
     bRight.Click+=new EventHandler(bRight_Click); 
     bCheck.Click+=new EventHandler(bZemaj_Click); 
     bReset.Click+=new EventHandler(bReset_Click); 
    } 

    private void bStart_Click(object sender, EventArgs e) 
    { 
     string Str = txtGoal.Text.Trim(); 
     int Num; 
     bool isNum = int.TryParse(Str, out Num); 
     if (isNum && Str.Length == 1) 
     { 
      string[] ts = txtVnes.Text.Split(
         new string[] { "\r\n" }, 
         StringSplitOptions.RemoveEmptyEntries); 
      int row = 0; 
      for (int i = 0; i < ts.Length && row < 6; i++) 
      { 
       if (LineIsValid(ts[i])) 
       { 
        for (int col = 0; col < 7; col++) 
        { 
         pole[row * 7 + col].Text = ts[i][2 * col].ToString(); 

        } 
        row++; 
       } 
      } 
      for (; row < 6; row++) 
      { 
       for (int col = 0; col < 7; col++) 
       { 
        pole[row * 7 + col].Text = "Z"; 
       } 
      } 
     } 
     else 
     { 
      MessageBox.Show("Invalid Input"); 

     } 
    } 

    private static Regex regex = new Regex(@"^(\s)*(\d){6}\d(\s)*$"); 

    private static bool LineIsValid(string line) 
    { 
    return regex.IsMatch(line); 

    } 

    private void bReset_Click(object sender, EventArgs e) 
    { 
      txtVnes.Clear(); 
      string[] ts = txtVnes.Text.Split(new string[] { "\r\n" }, 
      StringSplitOptions.RemoveEmptyEntries); 
      int row = 0; 
      for (int i = 0; i < ts.Length && row < 6; i++) 
      { 

        for (int col = 0; col < 7; col++) 
        { 
         pole[row * 7 + col].Text = "Z"; 
         pole[row * 7 + col].BackColor=Color.FromArgb(100, 149, 237); 
        } 
        row++; 

      } 


      for (; row < 6; row++) 
      { 
       for (int col = 0; col < 7; col++) 
       { 
        pole[row * 7 + col].Text = "Z"; 
        pole[row * 7 + col].BackColor = Color.FromArgb(100, 149, 237); 
       } 
      } 

      txtGoal.Clear(); 
      lblCovece.Location=new Point(160,165); 

    } 


    private void bUp_Click(object sender, EventArgs e) 
    { 
     lblCovece.Location = new Point(lblCovece.Location.X, lblCovece.Location.Y -  
      25); 

    } 

    private void bDown_Click(object sender, EventArgs e) 
    { 
     lblCovece.Location = new Point(lblCovece.Location.X, lblCovece.Location.Y + 
      25); 
    } 

    private void bLeft_Click(object sender, EventArgs e) 
    { 
     lblCovece.Location = new Point(lblCovece.Location.X - 25,    
     lblCovece.Location.Y); 
    } 
    private void bRight_Click(object sender, EventArgs e) 
    { 
     lblCovece.Location = new Point(lblCovece.Location.X + 25, 
      lblCovece.Location.Y); 
    } 
    private void bCheck_Click(object sender, EventArgs e) 
    { 


    } 


    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 




    } 
    } 
+1

문제의 원인 또는 위치가 명확하지 않습니다. 무엇이 잘못되었는지 (그리고 나서 기여하지 않는 다른 것들을 제거 할 수 있음)를 나타낼 수 있습니까? 일반적으로 이것은 디버깅 웹 사이트가 아닙니다. – rene

+0

그것은 나일지도 모르지만 당신은 당신의 질문을 바꿀 수 있습니다. 정확히 무엇을 묻고 있습니까? – Axis

+0

글쎄 실제로는 아무 것도 쓰지 않았습니다. 그게 문제입니다. bCheck_Click 이벤트에서 파트를 작성하는 방법을 모르겠습니다. 그리고 위에 체크 버튼이 무엇을해야하는지 설명했습니다. – Isabella

답변

0

int 점수를 추가하십시오.

private void bCheck_Click(object sender, EventArgs e) 
     { 
      bool found = false; 
      foreach (Label l in pole) 
      { 
       if (l.Location == lblCovece.Location && l.Text == txtGoal.Text) 
       { 
        l.BackColor = Color.Green; 

        score += int.Parse(l.Text); 
        lblResultE.Text = score.ToString(); 
        found = true; 
       } 
      } 
      if (!found) 
      { 
       score -= 10; 
       lblResultE.Text = score.ToString(); 
      } 
     } 
+0

예, 그렇습니다. 점수를 높이려면 어떻게합니까? 예를 들어, 2를 입력하고 배열을 실행하고 모든 2를 수집하면 결과는 2 + 2 + 2가되어야합니다. 얼마나 많은 2가 수집 되느냐에 따라 달라집니다. – Isabella

+0

작동하지 않습니다. 이 라인에서 유형 int를 문자열로 변환하십시오. lblResultE.Text = score ;. 또한 int 점수를 추가합니다. – Isabella

+0

lblResultE.Text = score.ToString(); – Axis

1

프로그래머를 복잡하고 이해하기 어렵게 만드는 이유는 게임 로직을 디스플레이 논리와 혼합하는 것입니다.

게임을 다시 디자인 해 보시기 바랍니다. 나는 (보드 등)뿐만 아니라이 될 수있는 매트릭스를 pole을 정의 할 양식에서

public enum State 
{ 
    Empty, // Displays "Z" 
    Neutral, // Blue 
    Good, // Green 
    Bad  // Red 
} 

public class Square 
{ 
    public int Number { get; set; } 
    public State State { get; set; } 
} 

public class Game 
{ 
    public const int Width = 7, Height = 6; 

    public Game() 
    { 
     Board = new Square[Width, Height]; 
    } 

    public event EventHandler GameChanged; 

    public Square[,] Board { get; private set; } 

    public int CurrentX { get; private set; } 
    public int CurrentY { get; private set; } 

    public void Reset() 
    { 
     for (int x = 0; x < Width; x++) { 
      for (int y = 0; y < Height; y++) { 
       Board[x, y].State = State.Empty; // Always displayed in blue as "Z" 
      } 
     } 
     OnGameChanged(); 
    } 

    public void MoveLeft() 
    { 
     if (CurrentX > 0) { 
      CurrentX--; 
      OnGameChanged(); 
     } 
    } 

    public void MoveRight() 
    { 
     if (CurrentX < Width - 1) { 
      CurrentX++; 
      OnGameChanged(); 
     } 
    } 

    // and so on 

    private void OnGameChanged() 
    { 
     EventHandler eh = GameChanged; 
     if (eh != null) { 
      eh(this, EventArgs.Empty); 
     } 
    } 
} 

: 그것은 다음과 같이 보일 수 있습니다. 변경 이벤트 GameChanged을 통해 일어날 때 게임 클래스는 양식을 알 방법

public class Form1 : Form 
{ 
    private Game game; 
    private Label[,] pole; 

    public Form1() 
    { 
     game = new Game(); 
     game.GameChanged += new EventHandler(Game_GameChanged); 

     pole = new Label[Game.Width, Game.Height]; 

     // Intialize pole. 
     // ... 
    } 

    void Game_GameChanged(object sender, EventArgs e) 
    { 
     for (int x = 0; x < Game.Width; x++) { 
      for (int y = 0; y < Game.Height; y++) { 
       Square square = game.Board[x, y]; 
       Label label = pole[x,y]; 
       switch (square.State) { 
        case State.Empty: 
         label.BackColor = Color.Blue; 
         label.Text = "Z"; 
         break; 
        case State.Neutral: 
         label.BackColor = Color.Blue; 
         label.Text = square.Number.ToString(); 
         break; 
        case State.Good: 
         label.BackColor = Color.Green; 
         label.Text = square.Number.ToString(); 
         break; 
        case State.Bad: 
         label.BackColor = Color.Red; 
         label.Text = square.Number.ToString(); 
         break; 
        default: 
         break; 
       } 
      } 
     } 

     // Place lblCovece according to game.CurrentX and game.CurrentY 
     // ... 
    } 

    private void bReset_Click(object sender, EventArgs e) 
    { 
     game.Reset(); 
    } 

    private void bLeft_Click(object sender, EventArgs e) 
    { 
     game.MoveLeft(); 
    } 

    private void bRight_Click(object sender, EventArgs e) 
    { 
     game.MoveRight(); 
    } 
} 

참고 : 난 당신이 무슨 뜻인지의 아이디어를주고, 양식 코드의 몇 관련 부분을 보여줍니다. 그런 다음 양식에서 게임 화면을 업데이트합니다. 게임 수업에서 이제 게임 논리에 집중할 수 있고 버튼과 라벨을 다룰 필요가 없습니다. 게임 보드의 [0..6]과 [0..5] 범위의 논리 좌표로 작업 할 수도 있습니다. 이것은 픽셀 작업보다 쉽습니다. 모든 픽셀 계산을 양식에 위임합니다.

예제가 완벽하지는 않지만 구현하려고하면 게임 논리가 어떻게 작동하는지 훨씬 쉽게 생각할 수 있습니다.

+0

그 중 절반은 이해하지만 다른 절반에서는 잃어 버렸습니다 :) 나는 이전 코드를 계속할 것입니다. 감사합니다. – Isabella

관련 문제