Winforms 程序生成表,使用事件时出现问题

Winforms 程序生成表,使用事件时出现问题,winforms,events,c#-4.0,.net-4.0,Winforms,Events,C# 4.0,.net 4.0,我正在用C语言为孩子们做一个程序,孩子们可以用它来测试他们对乘法表的知识。 我无法在Result_Leave()函数中获取“I”的值,以跟踪文本框数组的哪个值不正确 例如: 5 X 1=[]一种快速而肮脏的方法是在每个文本框的标记属性中设置一个值。在按钮内的for循环中单击,可以设置结果[i].Tag=i,然后在结果_Leave中,您可以执行以下操作: int number=(int)((发送者作为文本框).Tag),我想。 using System; using System.Collecti

我正在用C语言为孩子们做一个程序,孩子们可以用它来测试他们对乘法表的知识。 我无法在Result_Leave()函数中获取“I”的值,以跟踪文本框数组的哪个值不正确

例如:


5 X 1=[]一种快速而肮脏的方法是在每个文本框的
标记
属性中设置一个值。在
按钮内的for循环中单击
,可以设置
结果[i].Tag=i
,然后在
结果_Leave
中,您可以执行以下操作:

int number=(int)((发送者作为文本框).Tag)

,我想。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tables
{
    public partial class FormMain : Form
    {
        private System.Windows.Forms.Label[] labelNumber;
        private System.Windows.Forms.Label[] labelCross;
        private System.Windows.Forms.Label[] labelTableOf;
        private System.Windows.Forms.Label[] labelEquals;
        /*"Result" is an array of textboxes which takes the result of the multiplication from the user*/
        private System.Windows.Forms.TextBox[] Result; //declaration
        public FormMain()
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
            buttonCheckAnswers.Enabled = false;
        }

        private void buttonGo_Click(object sender, EventArgs e)
        {
            if (textBoxInput.Text == "")
            {
                errorProvider1.SetError(textBoxInput, "Hey! Enter a number please");
            }
            else
            {
                textBoxInput.Enabled = false;
                buttonCheckAnswers.Enabled = true;
                labelNumber = new System.Windows.Forms.Label[10];
                labelCross = new System.Windows.Forms.Label[10];
                labelTableOf = new System.Windows.Forms.Label[10];
                labelEquals = new System.Windows.Forms.Label[10];
                Result = new System.Windows.Forms.TextBox[10];
                for (int i = 0; i < 10; i++)
                {

                    // this snippet generates code for adding controls at runtime viz. textboxes and labels
                    labelNumber[i] = new Label();
                    this.labelNumber[i].AutoSize = true;
                    this.labelNumber[i].Location = new System.Drawing.Point(200, 163 + 55 * i);
                    this.labelNumber[i].Name = "labelNumber";
                    this.labelNumber[i].Size = new System.Drawing.Size(35, 13);
                    this.labelNumber[i].Text = (i + 1).ToString();
                    this.labelNumber[i].Font = new System.Drawing.Font("Comic Sans MS", 17F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.labelNumber[i].ForeColor = System.Drawing.Color.Khaki;
                    this.Controls.AddRange(new System.Windows.Forms.Control[] { labelNumber[i] });
                    labelCross[i] = new Label();
                    this.labelCross[i].AutoSize = true;
                    this.labelCross[i].Location = new System.Drawing.Point(150, 163 + 55 * i);
                    this.labelCross[i].Name = "labelCross";
                    this.labelCross[i].Size = new System.Drawing.Size(35, 13);
                    this.labelCross[i].Text = "X";
                    this.labelCross[i].Font = new System.Drawing.Font("Comic Sans MS", 17F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.labelCross[i].ForeColor = System.Drawing.Color.Khaki;
                    this.Controls.AddRange(new System.Windows.Forms.Control[] { labelCross[i] });
                    labelTableOf[i] = new Label();
                    this.labelTableOf[i].AutoSize = true;
                    this.labelTableOf[i].Location = new System.Drawing.Point(100, 163 + 55 * i);
                    this.labelTableOf[i].Name = "labelTableOf";
                    this.labelTableOf[i].Size = new System.Drawing.Size(35, 13);
                    this.labelTableOf[i].Text = textBoxInput.Text;
                    this.labelTableOf[i].Font = new System.Drawing.Font("Comic Sans MS", 17F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.labelTableOf[i].ForeColor = System.Drawing.Color.Khaki;
                    this.Controls.AddRange(new System.Windows.Forms.Control[] { labelTableOf[i] });
                    labelEquals[i] = new Label();
                    this.labelEquals[i].AutoSize = true;
                    this.labelEquals[i].Location = new System.Drawing.Point(250, 163 + 55 * i);
                    this.labelEquals[i].Name = "labelTableOf";
                    this.labelEquals[i].Size = new System.Drawing.Size(35, 13);
                    this.labelEquals[i].Text = "=";
                    this.labelEquals[i].Font = new System.Drawing.Font("Comic Sans MS", 17F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.labelEquals[i].ForeColor = System.Drawing.Color.Khaki;
                    this.Controls.AddRange(new System.Windows.Forms.Control[] { labelEquals[i] });

                    /*"Result" is an array of textboxes which takes the result of the multiplication from the user*/

                    Result[i] = new TextBox();
                    this.Result[i].BackColor = System.Drawing.Color.BlueViolet;
                    this.Result[i].Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.Result[i].ForeColor = System.Drawing.SystemColors.Info;
                    this.Result[i].Location = new System.Drawing.Point(300, 163 + 55 * i);
                    this.Result[i].Name = "Result" + i;
                    this.Result[i].Size = new System.Drawing.Size(57, 37);
                    this.Result[i].TabIndex = i;

                    /*this is where the problem arises...*/

                    this.Result[i].Leave += new System.EventHandler(this.Result_Leave);// how do I send the value of 'i' to Result_Leave() function
                    /*Note - Result_Leave() is FIRED when the cursor moves away from the "Result" textbox*/
                    this.Controls.AddRange(new System.Windows.Forms.Control[] { Result[i] });
                }
            }
        }

        private void textBoxInput_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.Clear();
        }
        private void radioButtonInstantChecking_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonCheckAtLast.Checked == true && textBoxInput.Text!="")
            {
                buttonCheckAnswers.Enabled = true;
            }
            else buttonCheckAnswers.Enabled = false;
        }
        private void Result_Leave(object sender, EventArgs e)
        {
            /*Code for checking multiplication goes here*/
            /*If multiplication result entered by the user is
             *correct change the background colour of the corresponding textbox "Result[i] to GREEN else BLUE"
             *as in buttonCheckAnswers_Click() function...
             */
        }

        private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -2)
                e.Handled = true;
        }

        private void buttonCheckAnswers_Click(object sender, EventArgs e)
        {
            int score=0;
            bool flag=false;
            for (int i = 0; i < 10; i++)
            {
                if (Result[i].Text == "")
                {
                    flag = true;
                    break;
                }
                else if ((Convert.ToInt32(Result[i].Text)) != ((Convert.ToInt32(labelNumber[i].Text) * (Convert.ToInt32(labelTableOf[i].Text)))))
                {
                    Result[i].BackColor = System.Drawing.Color.Red;

                }
                else
                {
                    Result[i].BackColor = System.Drawing.Color.Green;
                    score += 1;
                }
            }
            if (score == 10)
                labelComments.Text = "Well done kid! Full Marks!\nYou know your table of\n"+textBoxInput.Text+" very well"+"\nScore = "+score;
            else if(flag)
                labelComments.Text = "Oops! \nComplete your table kid!";
            else
                labelComments.Text = "Oops! \nThere are errors. \nPlease revise your tables!" + "\nYour score is : " + score;
        }
    }
}