2013-05-08 39 views
-1

我試圖使該計算保持時間和新聞界碼/按鍵的釋放時間通過鍵盤輸入。意外的文本(System.Int32 [])

hold time = keyup(i) - keydown(i+1) 
release_press time = keydown(i+1) - keyup(i) 
press_press time = keydown(i+1) - keydown(i) 
release_release time = keyup(i+1) - keyup(i) 

,這是我的代碼,既沒有錯誤,也沒有警告......但是當我運行它,我得到(System.Int32[])在文本框中,其中並同時按下/釋放時間應該展出。我不知道爲什麼我會收到錯誤或錯誤在哪裏。

Form1.cs的

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; 
    using Domainnn; 
    using Sessionnn; 


    namespace TheLatestKS 
    { 
     public partial class Form1 : Form 
{ 
    Brokerrr b = new Brokerrr(); 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    int i = 0; 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
     i++; 
    } 

    int[] A=new int[25]; 
    int[] B=new int[25]; 
    int[] C=new int[25]; 
    int[] D=new int[25]; 
    int[] M=new int[25]; 
    int[] N=new int[25]; 
    int[] O=new int[25]; 
    int[] P=new int[25]; 

    private void textBox2_KeyDown(object sender, KeyEventArgs e) 
    { 
     timer1.Enabled = true; 

     for (int a = 0; a < textBox2.TextLength; a++) 
     { 
      timer2.Enabled = true; 
      M[a] = k; 
      if (a >= 1) 
      { 
       O[a] = k; 
      } 
     } 
    } 

    int j = 0; 
    int s = 0; 
    float ave = 0; 
    private void button1_Click(object sender, EventArgs e) 

     { 
      timer1.Enabled = false; 
      textBox5.Text = i.ToString() + " ms"; 
      s = s + i; 
      Personnn p = new Personnn(); 
      p.Username = textBox1.Text; 
      p.Password = textBox2.Text; 
      p.Totaltyping = label5.Text; 
      p.Keyduration = textBox3.Text; 

      textBox4.Text += textBox2.Text + Environment.NewLine; 

      if (j >= 2) 
      { 
       ave = s/3; 
       textBox6.Text = ave.ToString() + " ms"; 
      } 

      p.Meanoftotal = textBox6.Text; 
      p.UDlatency1 = textBox8.Text; 
      p.DDlatency1 = textBox9.Text; 
      p.UUlatency1 = textBox10.Text; 

      b.Insert(p); 

      textBox1.Text = " "; 
      textBox2.Text = " "; 
      textBox3.Text = " "; 
      textBox8.Text = " "; 
      textBox9.Text = " "; 
      textBox10.Text = " "; 

      i = 0; 

      j++; 

     } 

    private void textBox1_KeyDown(object sender, KeyEventArgs e) 
    { 

     if ((e.KeyValue < 65 || e.KeyValue > 122) && (e.KeyValue != 32) && (e.KeyValue != 8)) 
     { 
      label7.Text = "Only letters are allowed"; 


     } 

     if (e.KeyValue == 8) 
     { 
      label7.Text = " "; 
     } 
    } 

    private void textBox2_KeyUp(object sender, KeyEventArgs e) 
    { 
     for (int a = 0; a < textBox2.TextLength; a++) 
     { 
      timer2.Enabled = true; 
      N[a] = k; 
      if (a >= 1) 
      { 
       P[a] = k; 
      } 

     } 

     for (int ii = 0; ii <textBox2.TextLength; ii++) 
     { 
     A[ii] = N[ii] - M[ii];// key duration 
     } 

     for (int iii = 0; iii < textBox2.TextLength-1; iii++) 
     { 
     B[iii] = O[iii] - N[iii];//UD latency 
     C[iii] = O[iii] - M[iii];//DDlatency 
     D[iii] = P[iii] - N[iii];//UU latency 
     } 

     textBox3.Text = A.ToString();// key duration 

     textBox8.Text = B.ToString();// UD latency 

     textBox9.Text = C.ToString();// DD latency 

     textBox10.Text = D.ToString();// UU latency 

    } 

    int k = 0; 
    private void timer2_Tick(object sender, EventArgs e) 
    { 
     k++; 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void label8_Click(object sender, EventArgs e) 
    { 

    } 

} 
    } 

Personnn.cs

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 

    namespace Domainnn 
    { 
public class Personnn 
{ 
    int id; 

    public int Id 
    { 
     get { return id; } 
     set { id = value; } 
    } 
    string username; 

    public string Username 
    { 
     get { return username; } 
     set { username = value; } 
    } 
    string password; 

    public string Password 
    { 
     get { return password; } 
     set { password = value; } 
    } 

    string totaltyping; 

    public string Totaltyping 
    { 
     get { return totaltyping; } 
     set { totaltyping = value; } 
    } 

    string meanoftotal; 

    public string Meanoftotal 
    { 
     get { return meanoftotal; } 
     set { meanoftotal = value; } 
    } 

    string keyduration; 

    public string Keyduration 
    { 
     get { return keyduration; } 
     set { keyduration = value; } 
    } 

    string UDlatency; 

    public string UDlatency1 
    { 
     get { return UDlatency; } 
     set { UDlatency = value; } 
    } 
    string DDlatency; 

    public string DDlatency1 
    { 
     get { return DDlatency; } 
     set { DDlatency = value; } 
    } 
    string UUlatency; 

    public string UUlatency1 
    { 
     get { return UUlatency; } 
     set { UUlatency = value; } 
    } 
} 
    } 

Brokerrr.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.OleDb; 
using Domainnn; 

namespace Sessionnn 
{ 
    public class Brokerrr 
    { 
     OleDbConnection connection; 
     OleDbCommand command; 

     private void ConnectTo() 
     { 
      connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\S.M.A.S\Documents\Visual Studio 2010\Projects\TheLatestKS\Databaseee.accdb"); 
      command = connection.CreateCommand(); 

     } 
     public Brokerrr() 
     { 
      ConnectTo(); 
     } 

     public void Insert(Personnn p) 
     { 
      try 
      { 
       command.CommandText = "INSERT INTO Table1 ([Username], [Password], Totaltyping, Meanoftotal, Keyduration, UDlatency, DDlatency ,UUlatency) VALUES('" + p.Username + "', '" + p.Password + "', '" + p.Totaltyping + "', '"+p.Meanoftotal+"', '"+p.Keyduration+"', '"+p.UDlatency1+"', '"+p.DDlatency1+"', '"+p.UUlatency1+"')"; 
       command.CommandType = CommandType.Text; 
       connection.Open(); 

       command.ExecuteNonQuery(); 
      } 
      catch (Exception) 
      { 

       throw; 
      } 
      finally 
      { 
       if (connection != null) 
       { 
        connection.Close(); 
       } 
      } 
     } 
    } 
} 
+2

這是非常* *醜陋的代碼。至少應該爲變量使用描述性名稱。你可能沒有甚至需要在這裏後,如果'A'被評爲像'KeyDurations'。 // MSDN: – Bobson 2013-05-08 16:24:45

回答

2

問題是與象下面的行:

textBox3.Text = A.ToString();// key duration 

ToString()方法不包含您的數字的字符串。它只是告訴你什麼類型。這是默認ToString行爲。

int s顯示數字,因爲它們有一個覆蓋的ToString方法。

你可能想

A[i].ToString(); 

或者你可能要做出某種數組,在這種情況下,你可以編寫一個函數或東西做的聚集串。

+0

如果您正在使用.NET 4.0或更高版本的LINQ語句是不必要的,'的string.join(」」,A)'會的工作由於[此重載(HTTP感謝ü山姆... – 2013-05-08 17:35:22

2

當你說

textbox3.Text = A.ToString(); 

是int數組上調用的ToString,由於INT []沒有一個重寫的ToString,它調用Object.ToString其打印類型名稱:的Int32 []

如果你想返回所有的元素,有各種方法,包括:

string.Join(" ", A) 
+0

的。 microsoft.com/en-us/library/dd992421.aspx) – 2013-05-08 16:47:16

+0

它沒有工作:(...我試過這個:var builder = new StringBuilder(); Array.ForEach(A,x => builder.Append (x)); var A = builder.ToString();但它沒有工作太 – 2013-05-08 17:39:13

+0

@ Scott很好,謝謝 – 2013-05-08 18:13:04