Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Enter Key C#

I have the following code which does not show the MessageBox when enter/return is pressed.

For any other key(i.e. letters/numbers) the MessageBox shows False.

private void cbServer_TextChanged(object sender, EventArgs e)
{
    if (enterPressed)
    {
        MessageBox.Show("Enter pressed");
    }
    else
        MessageBox.Show("False");
}

private void cbServer_Keydown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
    {
        enterPressed = true;
        MessageBox.Show("Enter presssed: " + enterPressed);

    }
    else
        enterPressed = false;
}

Any ideas?

EDIT: Above code, I thought the issue was with the _Keydown even so I only posted that.

like image 412
k1f1 Avatar asked Aug 16 '12 09:08

k1f1


People also ask

How do you check if the enter key is pressed in C?

Method 1. If you just detect enter key pressed at any time, use getchar() or cin. get() function. printf("Enter key is pressed"); Sleep(1000); //wait for check printed message.

How do you detect Enter press?

To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.

How do you take input until Enter is pressed in C?

We should use "%[^\n]", which tells scanf() to take input string till user presses enter or return key. Scanf scans till user presses enter or till user presses return key.

Does scanf read Enter?

scanf("%d", &b); The program will read in an integer value that the user enters on the keyboard (%d is for integers, as is printf, so b must be declared as an int) and place that value into b. The scanf function uses the same placeholders as printf: int uses %d.


1 Answers

This is because when you press Enter TextChanged event won't fire.

like image 132
Renatas M. Avatar answered Sep 25 '22 17:09

Renatas M.