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

C# 带有验证和操作的按钮事件,c#,events,button,C#,Events,Button,我需要有关检查按钮的帮助。用户在文本框中添加所有42个数字,并在“输入数字”区域中输入0-9之间的数字,然后单击“开始”按钮,接下来他应该做的是使用红色标签或lblCovece遍历标签数组,并收集与之前输入的数字相同的值。在他点击检查按钮后,程序应该首先验证用红色标签选择的值是否与输入的数字相同。如果有效,则标签应变为绿色,结果应显示在标签lblResultE中(例如,结果应如下:如果输入的数字为2,则结果为2+2+2…),如果在lblResultE中无效,则我们得10分。这就是我现在在帮助下所

我需要有关检查按钮的帮助。用户在文本框中添加所有42个数字,并在“输入数字”区域中输入0-9之间的数字,然后单击“开始”按钮,接下来他应该做的是使用红色标签或lblCovece遍历标签数组,并收集与之前输入的数字相同的值。在他点击检查按钮后,程序应该首先验证用红色标签选择的值是否与输入的数字相同。如果有效,则标签应变为绿色,结果应显示在标签lblResultE中(例如,结果应如下:如果输入的数字为2,则结果为2+2+2…),如果在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)
    {

    }




     }
  }
名称空间Seminarska
{
公共部分类Form1:Form
{
专用标签l、l2、lblCovece、l4、lblResultE;
私人按钮bUp、bRight、bLeft、bDown、bCheck、bStart、bReset;
私有文本框txtVnes,txtGoal;
自有品牌[]杆;
公共表格1()
{
初始化组件();
l2=新标签();
l2.Text=“输入一个数字”;
l2.位置=新点(230200);
l2.AutoSize=true;
l4=新标签();
l4.Text=“分数”;
l4.位置=新点(240260);
l4.AutoSize=true;
lblResultE=新标签();
lblResultE.Location=新点(350260);
lblResultE.AutoSize=true;
bLeft=新按钮();
bLeft.位置=新点(0,250);
bLeft.宽度=75;
高度=25;
bLeft.Text=“左”;
b检查=新建按钮();
b检查位置=新点(75250);
b检查宽度=75;
b检查高度=25;
bCheck.Text=“检查”;
bRight=新按钮();
位置=新点(150250);
明亮。宽度=75;
明亮,高度=25;
bRight.Text=“RIGHT”;
bUp=新按钮();
bUp.位置=新点(75220);
bUp.宽度=75;
bUp.高度=25;
bUp.Text=“向上”;
bDown=新建按钮();
b下降位置=新点(75280);
b宽度=75;
b下降高度=25;
bDown.Text=“向下”;
bStart=新建按钮();
b起始位置=新点(240165);
b起始宽度=75;
b起始高度=25;
bStart.Text=“开始”;
bReset=新按钮();
位置=新点(320165);
宽度=75;
高度=25;
bReset.Text=“重置”;
txtVnes=新文本框();
txtVnes.位置=新点(240,10);
txtVnes.宽度=160;
txtVnes.高度=130;
txtVnes.Multiline=true;
txtGoal=新文本框();
txtGoal.Width=75;
txtGoal.Height=25;
txtGoal.Location=新点(330200);
lblCovece=新标签();
lblCovece.Location=新点(160165);
lblCovece.宽度=20;
lblCovece.高度=20;
lblCovece.TextAlign=ContentAlignment.MiddleCenter;
lblCovece.Text=“O”;
lblCovece.BackColor=Color.FromArgb(255,0,0);
int a=0;
杆=新标签[42];
this.Controls.Add(lblCovece);
对于(int i=1;i添加一个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();
            }
        }

使您的程序复杂且难以理解的是,您将游戏逻辑与显示逻辑混合在一起

我建议你重新设计你的游戏。它可能看起来像这样:

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
发生更改时,游戏类如何告知表单。表单随后会更新游戏显示。在游戏类中,您现在可以专注于游戏逻辑,而不必处理按钮和标签。您还可以使用[0..6]和[0..5]范围内的逻辑坐标这比使用像素更容易。您可以将所有像素计算委托给窗体


我的例子还不完整,但当你尝试实现它时,你会发现思考游戏逻辑应该如何工作要容易得多。

不清楚问题是什么或在哪里。你能指出哪一行出了问题(然后可能删除其他不起作用的东西)。一般来说,这不是一个调试网站。它可能只是我,但你能重新表述你的问题吗。你到底在问什么?事实上,a没有写任何东西,这就是问题所在。我不知道如何在bCheck_Click事件中写该部分。我在上面解释了检查按钮应该做什么。当来自txtVnes的值在标签数组中复制时,具有上、左、右和下按钮的用户应移动标签lblCovece(红色标签),并应查找他之前在txtGoal中输入的相同值。单击按钮检查后,如果LBLCove与标签数组中的标签重叠,且该标签的值与在txtGoal中输入的值相同,则必须在lblResultE标签中显示的结果对于输入和收集的值应增加d、 是的,这是我的观点,但是我如何增加分数呢?例如,如果我输入2,在数组中运行并收集所有2,结果应该是2+2+2…,这取决于我将收集多少个2。它不起作用,它表示无法将这一行中的类型int转换为字符串lblResultE.Text=score;。我还添加int-score;lblResultE.Text=score.toStringg();好的,但它给了我很高的分数(负数):-403…-736…-1069…(当我输入7时就会发生这种情况)。一半我理解,但另一半我迷路了:)我想我将继续前面的代码。谢谢
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();
    }
}