Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I tried this code:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Convert.ToInt32(e.KeyChar) == 13) 
    {
        MessageBox.Show(" Enter pressed ");
    }
}

and this:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == Convert.ToChar(Keys.Enter))
    {
        MessageBox.Show(" Enter pressed ");
    }
}

and this:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == Keys.Enter)
    {
        MessageBox.Show(" Enter pressed ");
    }
}

but they're not working...

When I write something and press Enter, it doesn't work. It only highlights my text.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
392 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this code,might work (Assuming windows form):

private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
    {
        // Enter key pressed
    }
}

Register the event like this :

this.textBox1.KeyPress += new 
System.Windows.Forms.KeyPressEventHandler(CheckEnter);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...