C # KeyEvent does not register input / return key

I am doing this registration form in C #, and I wanted to β€œsend” all the data as soon as the user either clicks the β€œSend” button or presses the enter / return key.

I tested KeyEvents a bit, but nothing worked.

void tbPassword_KeyPress(object sender, KeyPressEventArgs e) { MessageBox.Show(e.KeyChar.ToString()); } 

The code above was supposed to check if the event really worked in the first place. It works great when I press "d", it shows me "d", when I press "8", it shows me "8", but pressing the enter key does nothing.

So, although it was because the attachment was not tied to a character, but it displayed backspace, it worked fine, so I was confused why it did not register my input key.

So the question is: How do I enter the input / return key? and why is it not registering a keystroke right now, as it should?

Note: I put this event in a text box

 tbPassword.KeyPress += new KeyPressEventHandler(tbPassword_KeyPress); 

Thus, it works when you press the WHILE enter button, when the text field is selected (this has been the whole time, of course), maybe something has to do with the code execution.

+4
source share
6 answers

Do you have a button defined as the default action?

If so, this control will burn the Enter key.

And perhaps this is your answer. You need to set the SubmitAction property to true in your submit button.

+5
source

Try to execute the KeyDown event.

 private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { MessageBox.Show("Enter"); } } 
0
source

Perhaps you should use the "AcceptButton" form to set it to the submit button. Think this is what you really ...

0
source

You have abandoned the vital bit, you must set the Handled property to true or false depending on the condition ...

  void tbPassword_KeyPress (object sender, KeyPressEventArgs e)
 {
     MessageBox.Show (e.KeyChar.ToString ());
     if (e.KeyCode == Keys.Enter) {
       // This is handled and will be removed from Windows message pump
       e.Handled = true; 
     }
 }
0
source

try it

 textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '\r') { MessageBox.Show("Enter Key Pressed", "Enter Key Pressed", MessageBoxButtons.OK); } } 
0
source

go to your forms ...

in the main form, change this value

FormName.AcceptButton = buttonName;

this will read the input key log file ... automatically ..

you can do this if you do not want users to see the confirmation button

buttonName.Visible = false; FormName.AcceptButton = buttonName;

AcceptButton Automatically Reads Keyboard Input Key

0
source

Source: https://habr.com/ru/post/1304654/


All Articles