I have a user authentication form with username and password textboxes.There is an okay button that fires the code to validate the credentials.
I want the same code to get executed when the user hits Enter key anywhere on the form.
So i register for the keypress event like this
this.KeyPress += UserLogin_KeyPress;
private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
MessageBox.Show("enter");
}
}
This event is not triggered at all.What i'm i doing wrong?
Try setting the property keypreview to true and changing to keydown instead since KeyPress don't support e.Keycode:
private void UserLogin_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter");
}
}
Try this:
private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("enter");
}
}
As mentioned, set form.KeyPreview = true so that most key events will be given to the form before the controls.
However, note that this doesn't always work, and some controls will still "steal" certain key presses anyway. For example, if a Button has focus, then an Enter keypress will be used by the button to activate it, before you get a chance to see it, and since it counts as having been "handled", you never get to see the keypress.
The workaround I use for this is to set focus to something that I know will not steal the enter key press, for key presses that occur leading up to the enter key press. So, if the pressed keys are <1><2><3><4><ENTER> then the 1-4 keys all set focus to something that is not a button (usually, the textbox where I am displaying the resulting text), and then when <ENTER> is pressed, it should make it to your form's event handler (as long as KeyPreview == true).
It's only looking at the form. Controls on the form also need to be wired in.
Related
I have a button for which I set the KeyPress event.
this.myButton.KeyPress += new KeyPressEventHandler(this.myButtonEvent_keypress);
private void myButtonEvent_keypress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return || e.KeyChar == (char)Keys.Space)
{
// do something
}
}
Now whenever the Space key is pressed, I get the event triggered. This part works fine.
But for some reason, the Enter key press is not triggering the KeyPress
event. Also Alt, Ctrl, Shift are not working.
How can I make the button receive Enter key press?
UPDATE:
I tried below too without any luck
this.myButton.MouseClick += new MouseEventHandler(myButton_Click);
When a Button has focus and you press Enter or Space the Click event raises.
So to handle Enter or Space it's enough to handle Click event and put the logic you need there.
So you just need to use button1.Click += button1_Click;
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked!");
}
If you really want to know if Enter or Space was pressed, you can hanlde PreviewKeyDown event:
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if(e.KeyCode== Keys.Enter)
{
MessageBox.Show("Enter");
}
if (e.KeyCode == Keys.Space)
{
MessageBox.Show("Space");
}
}
Enter and space can be handled using click event.
this.myButton.Click += new EventHandler(myButton_Click);
The Button control in WinForms will eat Enter, Space, Tab, ESC, and a few other special key's press events. One method to intercept these events is to override Control.ProcessDialogKey. Or you can override IsDialogKey to say if a key should be handled as a special case.
A another option is to set KeyPreview = true on you parent Form. Then you can handle all KeyPress events at the Form level and use Form.ActiveControl if you need to see what control has Focus
I'm using some richTextBoxes in one page of my form, in keyDown of richTextBoxes, i wrote if Ctrl+Enter is pressed, sendKeys the Tab key so the next one get focus, and I also have a keyDown event on whole form, so when user press Ctrl+Tab, selected page change to next page
problem is, when i press Ctrl+Enter on the richTextBoxes, keyDown of form with Ctrl+Tab happens and tab changes, why it sees enter and tab alike ?
Sorry for my bad grammar, and thanks for your effort
keyDown event of Main Form:
private void Main_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Control)
{
//code for changing the tabs
}
}
keyDown event of richTextBoxes:
private void txtControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Enter)
{
SendKeys.Send("{Tab}");
e.Handled = true;
e.SuppressKeyPress = true;
}
}
Usually, when you press a key, the focused control receives the key pressed. You could change a bit of this flow setting the Form.KeyPreview property to True. With this setting the Form receives the key pressed before the focused control.
You are messing with this 'normal' flow trying to insert a fake TAB key while you are processing the KeyDown event that has reached the RichTextBox keydown event handler. You suppress the CTRL+ENTER key but, at the exit of the event, the TAB key is received by the form with the Control bit still set and, as far as I know, there is no easy way to reset this bit, hence you process a CTRL+TAB in your form keydown.
Fortunately all of this is not needed, you set the next control in tab order line simply with
private void txtControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Enter)
{
Control current = sender as Control;
this.SelectNextControl(current, true, true, true, true);
e.SuppressKeyPress = true;
}
}
The call to the form method SelectNextControl gives you a lot more control on what should happen and doesn't insert a fake TAB key in the keyboard processing.
You send tab SendKeys.Send("{Tab}"); on keydown. The modifier is stil CTRL. Than the keydown of the MainForm in focus catches Tab. Stop sending SendKeys.Send("{Tab}");?
I have a UserControl for an application and with a textbox. When pressing enter in this textbox a button should be activated (basically pressing 'Ok' via pressing Enter instead of clicking the button manually with the mouse).
So I added a KeyDown event to my textbox and implemented the following code:
private void txtSearchID_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && !txtSearchID.Text.Equals(string.Empty))
{
button_Click(null, null);
}
}
But when I wanted to test this nothing happened when pressing Enter. Strangely enough in addition to that the event does not even get fired when pressing Enter but with every other key. Am I missing something?
This is because, when a key is pressed it will go to the control which has focus on the form, as the KeyPreview property of Form is set to False by default. You change the event like this
Change to KeyUP event,
private void txtSearchID_Keyup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && !txtSearchID.Text.Equals(string.Empty))
{
button_Click(null, null);
}
}
I have a form with various text boxes. One text box is used for entering a floating point number, so I am using TextBox.KeyPress to process each digit in turn, which only modifies the Text property. The text is processed by a routine that is called when the OK button id pressed (before closing the form). It is also called by the TextBox.Leave event. However, if I change the contents of the text box then press Return to variable isn't updated.
I thought I could overcome this by the following:
private void DestPointNoTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
prvUpdateDestPointNo();
}
}
This is called whenever a key is pressed while the text box is in focus, as can be proved by setting a breakpoint within it. However, it is not called when Return is pressed.
Can someone explain how I can ensure new text is processed when Return is pressed?
If I change the contents of one then click the OK button the new
I would use the Debugger to determine the value of e.KeyCode at runtime.
Why?
When I ran similar code (Winforms TextBox, KeyDown registered), the value of e.KeyCode was
e.KeyCode = LButton | MButton | Back
Another property of KeyEventArgs you can use is KeyValue -
private void DestPointNoTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
prvUpdateDestPointNo();
}
}
If you are using Windows Form Application, then you can set the AcceptButton property of the Form to the OK button and the Click event of the OK button will be fired when you press Enter/Return key
I've been making this login form in C# and I wanted to 'submit' all the data as soon as the user either clicks on submit or presses the enter/return key.
I've been testing a bit with KeyEvents but nothing so far worked.
void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
The above code was to test if the event even worked in the first place.
It works perfectly, when I press 'd' it shows me 'd' when I press '8' it shows me '8' but pressing enter doesn't do anything.
So I though this was because enter isn't really bound to a character but it did show backspace, it worked just fine so it got me confused about why it didn't register my enter key.
So the question is:
How do I log the enter/return key? and why doesn't it log the key press right right now like it should?
note: I've put the event in a textbox
tbPassword.KeyPress += new KeyPressEventHandler(tbPassword_KeyPress);
So it fires when the enter button is pressed WHILE the textbox is selected (which is was the whole time of course) maybe that has something to do with the execution of the code.
Do you have a button defined as the default action?
If so then that control will gobble up the Enter key.
And maybe that is your answer. You need to set the DefaultAction property to true on your submit button.
Try the KeyDown event instead.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter");
}
}
Perhaps you should use the "AcceptButton" of the form to set it to the submit button. Think that is what you what really...
You have left out a 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;
}
}
Try this
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);
}
}
go to your forms...
in the basic form change this
FormName.AcceptButton = buttonName;
this would read the key log file of enter... automatically..
you can do this if you dont want users to see accept button
buttonName.Visible = false;
FormName.AcceptButton = buttonName;
AcceptButton automatically reads the enter key from the keyboard