简体   繁体   English

带有验证和操作的按钮事件

[英]button event with validation and actions

I need help with the Check button. 我需要“检查”按钮的帮助。 After a user adds all the 42 numbers in the textbox and enter a number from 0-9 in the "Enter number" area and clicks on the start button, next what he should do is run through the array of labels with the red label or lblCovece and he should collect the same values like the number enters before. 用户在文本框中添加所有42个数字,然后在“输入数字”区域中输入0-9之间的数字,然后单击开始按钮,接下来他应该做的是遍历带有红色标签或lblCovece,他应该收集与之前输入的数字相同的值。 And after he clicks on the Check button, the programme should first validate if the value that is selected with the red label is the same as the number entered. 在他单击“检查”按钮后,程序应首先验证用红色标签选择的值是否与输入的数字相同。 If is valid the label should turn green and than the result should appear in the label lblResultE(the result for example should be like this: if the number entered is 2, the result it is 2+2+2...)and if is not valid in the lblResultE we take out 10 points. 如果有效,则标签应变为绿色,然后结果应显示在标签lblResultE中(例如,结果应如下所示:如果输入的数字为2,则结果为2 + 2 + 2 ...),如果在lblResultE中无效,我们得10分。 That's what i did by now with some help.:) thank you. 这就是我现在在一些帮助下所做的。:)谢谢。

  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)
    {

    }




     }
  }

What makes your programm complicated and difficult to understand, is that you mix game logic with display logic. 使程序复杂且难以理解的原因是,您将游戏逻辑与显示逻辑混合在一起。

I suggest you to redesign your game. 我建议您重新设计游戏。 It could look something like this: 它可能看起来像这样:

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);
        }
    }
}

In the form I would define pole to be a matrix as well (like the board). 在表格中,我还将pole定义为矩阵(例如板子)。 I show only a few relevant parts of the form code, to give you an idea of what I mean: 我只显示了表单代码的一些相关部分,以使您了解我的意思:

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();
    }
}

Note how the Game class tells the form when changes happen through the event GameChanged . 注意Game类如何通过事件GameChanged通知表单何时发生更改。 The form then updates the game display. 窗体然后更新游戏显示。 In the game class, you can now concentrate on the game logic and do not have to deal with buttons and labels. 在游戏类中,您现在可以专注于游戏逻辑,而不必处理按钮和标签。 You can also work with logical coordinates in the range [0..6] and [0..5] of the game board. 您还可以使用游戏板的[0..6]和[0..5]范围内的逻辑坐标。 This is easier than working with pixels. 这比处理像素要容易。 You delegate all the pixel calculations to the form. 您将所有像素计算委托给表单。

My example is not complete, but when you try to implement it, you will see that it will be much easier to think about how the logic of the game should work. 我的示例还不完整,但是当您尝试实现它时,您会发现考虑游戏逻辑的工作原理要容易得多。

Add an int score; 加上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();
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM