开发者

Program to generate Tables, trouble using Events

开发者 https://www.devze.com 2023-03-10 15:11 出处:网络
I am doing a program in C# for kids with which kids can test their knowledge of Multiplication tables.

I am doing a program in C# for kids with which kids can test their knowledge of Multiplication tables. I cannot get the value of 'i' in the Result_Leave() function to track which value of the text box array is incorrect.

For Example:

5 X 1 = [ ] <---- an array of textboxes with name Result[i]"

the user enters the value of 5*1 in the text box and my program instantly checks if the entered value is correct or not, using the "Leave" event for the text box. I have used an array of text boxes... So I cannot track which Result[i] contains the incorrect value... I have fired the function "Result_Leave" for each of the text boxes "Result[i]"

Here is the code:

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;
        }
        priv开发者_开发技巧ate 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;
        }
    }
}


One quick-and-dirty way to do this is to set a value in each TextBox's Tag property. In the for loop inside buttonGo_Click, you could set Result[i].Tag = i;, then in Result_Leave you could do:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消