3 Replies - 584 Views - Last Post: 04 June 2011 - 03:41 PM Rate Topic: -----

#1 arnabkaycee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-October 10

Program to generate Tables, trouble using events

Posted 04 June 2011 - 05:07 AM

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 = [ <blank text box> ] <---- an array of textboxes with name Result[i]"
5 X 2 = [ <blank text box> ]
5 X 3 = [ <blank text box> ]
5 X 4 = [ <blank text box> ]
.
......

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



Is This A Good Question/Topic? 0
  • +

Replies To: Program to generate Tables, trouble using events

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Program to generate Tables, trouble using events

Posted 04 June 2011 - 05:20 AM

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...
     */
}


The sender in this event handler is the TextBox that fired the event. You can access the TextBox control in there by casting the sender to a TextBox object and then you can use the properties of said object.
Was This Post Helpful? 1
  • +
  • -

#3 arnabkaycee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-October 10

Re: Program to generate Tables, trouble using events

Posted 04 June 2011 - 07:06 AM

How do I get the value of 'i' i.e array index of the selected/focussed text box?
Was This Post Helpful? 0
  • +
  • -

#4 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Program to generate Tables, trouble using events

Posted 04 June 2011 - 03:41 PM

Hey, I did a couple learning apps too. One pointer I have is, don't call it "result" call it "response." Result can be ambiguous when comparing it with, say, the actual result of 1x1.

Also, you might want to consider implementing your gui in HTML/javascript for portability, and beautification.

Here's some code I came up with:


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...
             */
            int q = Convert.ToInt32(((TextBox)sender).Text);  // get the response (as an int)

            MessageBox.Show("did it: " + q.ToString());

            string istr = ((TextBox)sender).Name.TrimStart(new char[] { 'R', 'e', 's', 'u', 'l', 't'});   // get i,     LOL, maybe someone else has a smoother way of doing this.  Your algo isn't anything I would have imagined on my own so I'm at a disadvantage.
            int i = Convert.ToInt32(istr);

            //Result[Convert.ToInt32(istr)].Text = "0";                                      // You now have complete access to the response... yum... but we don't need that too much



            //  CORRECTION ROUTINE  (flaws: does not divide/ account for operator (btw, rename 'cross' to 'operator')

            switch (labelCross[i].Text)
            {
                case "X":
                    {
                        int A;
                        int B;
                        double correctAnswer;

                        A = Convert.ToInt32(labelNumber[i].Text);
                        B = Convert.ToInt32(labelTableOf[i].Text);

                        correctAnswer = A * B;

                        if (((TextBox)sender).Text != Convert.ToString(correctAnswer))
                        {
                            MessageBox.Show("bad");
                            // mark incorrect
                        }
                        else
                        {
                            MessageBox.Show("good");
                            // good job
                            // mark correct
                        }
                        break;
                    }
                case "/":
                    {
                        MessageBox.Show("warning:  Programmer lazieness");
                        break;
                    }
            }

        }



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1