Question

I am working with c# windows application and i need to enter some record to database after inserting in to textbox. I have tried this code

private void textBoxItemCode_KeyDown(object sender, EventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
       MessageBox.Show("You have entered the correct key.");
    }

}

but i am getting an error near e.KeyCode so how to make it

Était-ce utile?

La solution 2

It Should be like this :-

private void textBoxItemCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        MessageBox.Show("You have entered the correct key.");
    }

}

Autres conseils

You need to use the following args

System.Windows.Forms.KeyEventArgs

Your handler should look like this

private void textBoxItemCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        MessageBox.Show("You have entered the correct key.");
    }

}

That way you have access to the KeyCode property which does not exist on the base EventArgs

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top