2011-06-04 54 views
0

我正在做一個C#程序,讓孩子們可以測試他們的乘法表知識。 我無法在Result_Leave()函數中獲取'i'的值來跟蹤文本框數組的哪個值不正確。程序生成表,使用事件發生故障

例如:

5 X 1 = [] < ----文本框的陣列與名稱結果[I]」

用戶輸入的5 * 1在文本框中的值我的程序立即檢查輸入的值是否正確,使用文本框的「離開」事件 我已經使用了一個文本框的數組... 所以我不能跟蹤哪個結果[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; 
     } 
    } 
} 
+0

[值得一讀(http://tinyurl.com/so-hints),我猜。 – 2011-06-04 12:04:19

回答

0

一個快速和骯髒的方式做,這是設置在每個文本框的Tag屬性的值。在對內部buttonGo_Click循環,你可以設置Result[i].Tag = i;,然後在Result_Leave你可以這樣做:

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