2013-09-26 72 views
3

我有一個註冊應用程序,裏面有「KeyChar」事件,它效果很好!但是當我在這個應用程序中給出相同的代碼行時,它給我Operator '=='/'!=' cannot be applied to operands of type 'char' and 'string'運算符'=='不能應用於'char'和'string'類型的操作數

似乎無法弄清楚它爲什麼在另一個應用程序中工作,但不在這裏! 任何幫助非常感謝!

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand(); 

     Object returnValue; 

     string txtend = textBox1.Text; 
     //string lastChar = txtend.Substring(txtend.Length - 1); 

     if (e.KeyChar == "L") 
     { 
      DBConnection.Open(); 
     } 
     if (DBConnection.State == ConnectionState.Open) 
     { 
      if (textBox1.Text.Length != 7) return; 
      { 
       //cmd.CommandText = ("SELECT last_name +', '+ first_name +'\t ('+major_key+')\t' from name where id [email protected]"); 
       cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id [email protected]"); 
       cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", ""))); 
       cmd.CommandType = CommandType.Text; 
       cmd.Connection = DBConnection; 
       // sqlConnection1.Open(); 
       returnValue = cmd.ExecuteScalar() + "\t (" + textBox1.Text.Replace(@"L", "") + ")"; 
       DBConnection.Close(); 

       if (listBox1.Items.Contains(returnValue)) 
       { 
        for (int n = listBox1.Items.Count - 1; n >= 0; --n) 
        { 
         string removelistitem = returnValue.ToString(); 
         if (listBox1.Items[n].ToString().Contains(removelistitem)) 
         { 
          listBox1.Items.RemoveAt(n); 
          //listBox1.Items.Add(removelistitem+" " +TimeOut+ Time); 
         } 

        } 
       } 
       else 
        listBox1.Items.Add(returnValue); 

       System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fullFileName); 
       foreach (object item in listBox1.Items) 
        SaveFile.WriteLine(item.ToString()); 
       SaveFile.Flush(); 
       SaveFile.Close(); 

       textBox1.Clear(); 

       if (listBox1.Items.Count != 0) { DisableCloseButton(); } 
       else 
       { 
        EnableCloseButton(); 
       } 
       Current_Attendance_Label.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance."; 
       e.Handled = true; 
      } 
     } 
     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
     else 
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
     { 
      returnValue = textBox1.Text.Replace(@"*", ""); 

      if (e.KeyChar == "*") return; 
      { 
       if (listBox1.Items.Contains(returnValue)) 
       { 
        for (int n = listBox1.Items.Count - 1; n >= 0; --n) 
        { 
         string removelistitem = returnValue.ToString(); 
         if (listBox1.Items[n].ToString().Contains(removelistitem)) 
         { 
          //listBox1.Items.RemoveAt(n); 
         } 
        } 
       } 
       else 
        listBox1.Items.Add(returnValue); 
       textBox1.Clear(); 
       System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fullFileName); 
       foreach (object item in listBox1.Items) 
        SaveFile.WriteLine(item.ToString()); 
       SaveFile.Flush(); 
       SaveFile.Close(); 
       if (listBox1.Items.Count != 0) { DisableCloseButton(); } 
       else 
       { 
        EnableCloseButton(); 
       } 
       Current_Attendance_Label.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance."; 
       e.Handled = true; 
      } 
     } 
    } 

回答

16

使用該字符。單引號'定義character,而雙引號"定義string

if (e.KeyChar == 'L') 
+0

不能相信我錯過了!非常感謝你! –

+6

我也學到了很難:) –

+0

好抓住... :) – Ashok

相關問題