3

I have a registration application which has the "KeyChar" event inside it, and it works great ! but when i give the same lines of code in this application it gives me Operator '=='/'!=' cannot be applied to operands of type 'char' and 'string'

Can't seem to figure out why it works in the other application but not here! Any help is much appreciated !

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

1 Answer 1

18

Use the character. Single quotes ' define a character, while double quotes " define a string:

if (e.KeyChar == 'L')
Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe I missed that! Thank You Very Much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.