C# 操作员'==';无法应用于类型为';char';和';字符串';
我有一个注册应用程序,里面有“KeyChar”事件,它工作得很好!但是,当我在这个应用程序中给出相同的代码行时,它会给出C# 操作员'==';无法应用于类型为';char';和';字符串';,c#,winforms,C#,Winforms,我有一个注册应用程序,里面有“KeyChar”事件,它工作得很好!但是,当我在这个应用程序中给出相同的代码行时,它会给出运算符'=='/'!='无法应用于“char”和“string”类型的操作数。 似乎无法理解为什么它在其他应用程序中工作,但在这里不工作! 非常感谢您的帮助 private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { SqlConnection DBConnection =
运算符'=='/'!='无法应用于“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 =@Name");
cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id =@Name");
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;
}
}
}
使用字符。单引号
”
定义字符
,而双引号“
定义字符串
:
if (e.KeyChar == 'L')
真不敢相信我错过了!非常感谢!