Search code examples
c#winformskeypress

Visual Studio c# Key press enter not working


Hey guys I've been struggling with my code ! I did some research and I don't understand why my code isn't working... Please help !

private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)13)
        {
            Debug.WriteLine("It's working!");
            enterKey = true;
        }
        else
        {
            enterKey = false;
        }
    }

    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        Debug.WriteLine("The text is changing");

        if (enterKey == true)
        {
            encryptKey = encryptInTextBox.Text;
            Debug.WriteLine("The key is " + encryptKey);
        }
    }

Apparently I can't change 'TextChangedEvenArgs' because of how the text box was created, whenever I change it, it comes with an error. So, I decided to do it this way, thanks for the help !


Solution

  • Why don't you check the ENTER key directly into the textbox KEYPRESS or KEYUP event?