用于生成表的程序,使用事件的麻烦

时间:2011-06-04 12:00:49

标签: winforms events c#-4.0 .net-4.0

我正在用C#为孩子们做一个程序,让孩子们可以测试他们对乘法表的了解。 我无法在Result_Leave()函数中获取'i'的值来跟踪文本框数组的哪个值不正确。

例如:

5 X 1 = []< ----一个名为Result [i]“

的文本框数组

用户在文本框中输入值5 * 1,我的程序使用文本框的“Leave”事件立即检查输入的值是否正确。 我使用了一系列文本框...... 所以我无法跟踪哪个Result [i]包含不正确的值... 我为每个文本框“Result [i]”

触发了函数“Result_Leave”

以下是代码:

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

1 个答案:

答案 0 :(得分:0)

这样做的一种快捷方法是在每个TextBox的Tag属性中设置一个值。在buttonGo_Click内的for循环中,您可以设置Result[i].Tag = i;,然后在Result_Leave中设置:{/ p>

int number = (int)((sender as TextBox).Tag);