2012-01-08 7 views
0

[チェック]ボタンのヘルプが必要です。ユーザーがテキストボックスに42の数字をすべて追加し、「番号を入力」エリアに0〜9の数字を入力してスタートボタンをクリックすると、次に赤いラベルのラベルの配列を実行するか、 lblCoveceと同じ値を収集する必要があります。そしてチェックボタンをクリックした後、赤いラベルで選択された値が入力された値と同じであるかどうかを確認する必要があります。有効な場合はラベルが緑色に変わり、lblResultEというラベルに結果が表示されます(結果は例えば次のようになります:入力された数字が2なら結果は2 + 2 + 2 ...)if 10ポイントを取るlblResultEでは有効ではありません。それは私が今いくつかの助けを借りて行ったことです:)。ありがとう。妥当性検査とアクションを含むボタンイベント

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イベントでパーツを書き込む方法がわかりません。上にCheckボタンの機能について説明しました。 – 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

関連する問題