How to capture Key press event for a text box? - c#

I have a winform application which has two text boxes and a button. If the control focus is on any of the text boxes and user clicks keyboard "enter" button. The button event should invoke.
The issue is I couldn't find the TextBox_KeyDown to capture the "Enter" key press. In the Visual Studio editor, KeyDown,KeyPress,Keyup properties are empty.

A couple things. It sounds to me like you just need to set the AcceptButton on the Form to the button you want clicked when the user presses Enter. It will be handled automatically for you.
Second, if that's not the case, then you need to set the KeyPreview to true on the Form and handle the ProcessCmdKey method on the Form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
// do something
}
else
{
base.ProcessCmdKey(ref msg, keyData);
}
}

you can do it like this?
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
}
}

Related

C# UserControl keyData Only working after pressing a button first in windows forms

Buttons working fine as expected by clicking them.
Issue: When the UserControl is loaded for the first time and i didn't press any button in it, the Keydata are not working.
After clicking a button manually the keybinds do work as intended. So obviously i would like to let the user use the keybind before any button press :
(I already tried to set focus on different elements such as the button itself)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Enter:
button1.PerformClick();
return true;
case Keys.Escape:
button2.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
i would like to let the user use the keydata before any button press :
Just Focused usercontrol, from main form button
private void label1_Click(object sender, EventArgs e)
{
usercontrol11.BringToFront();
usercontrol11.Focus();
}

How do I bind a keyboard key to a label

I am trying to bind a keyboard button (preferably "ESC") to stop the code that is running inside the method. But the thing is, it only works with actual buttons, is there anyway to bypass this so it works when pressing labels aswell?
private void label1_Click(object sender, EventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "ESC")
{
MessageBox.Show("This does now work");
}
}
I've read somewhere that it is possible its just that the Visual Studio GUi doesnt provide it, but you can do it with code somehow, is this true?
I am not sure what you are trying to do. The following will get the Escape key when it is pressed when the focus is in the form regardless of what control has the focus. Just add this to the form (the code of course).
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Escape))
{
MessageBox.Show("This works");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

C#: Perform PictureBox.Click if pressed enter

Here is my code.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
loginButton.Click; //PictureBox & It doesn't work.
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
How to fix this? Been searching around but I couldn't find a correct one. Thanks in advance.
Here is how my form looks.
loginButton.Click is the event handler. You should call loginButton.PerformClick();
loginButton_Click(new object(), new EventArgs());
try this one to replace to your loginButton.Click
Set the AcceptButton property of your form to be the button you want clicked when enter is pressed. This will also have the effect of providing the outline around the button that indicates to the user that if they press enter, it will be the same as clicking that button.
You can also set this through code in your constructor (or elsewhere if need be) by doing
public MyForm()
{
InitializeComponent();
AcceptButton = loginButton;
}

Programmatically changed TAB key issue - c# window application

I have set the "ENTER" key as a "TAB" key in windows form.
following is the code
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
KeyEventArgs e = new KeyEventArgs(keyData);
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
}
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}
its working perfectly with all controls but when I press ENTER on any combobox then it takes two tab indexes.
eg.
textbox1
combobox
textbox2
textbox3
textbox4
when i press ENTER to leave combobox then the cursor(FOCUS) is directly comes to the textbox 3
i want that focus to the next control of combobox , i.e, textbox 2
please help me
thanks in advance..!
Just return true; after you send the Tab:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
KeyEventArgs e = new KeyEventArgs(keyData);
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
return true;
}
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}
Control.TabIndex property decides that which order the control should get focused when a Tab key is pressed.
You should set TabIndex properties of controls in a order that you would like them to focus based on Tab key press.
That said, you shouldn't use SendKeys.Send("{TAB}") method for simulating a Tab key press. You should use Control.SelectNextControl method for this purpose.

Stop Winform keystroke from reaching child control

I have a form that has several buttons. I have KeyPreview set to true, and I have Keydown, KeyPress, and keyup events all reading
form_keyevent(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.handled = true;
}
For some reason, the enter key still clicks a button that has focus. What am I missing? Is there a way around it?
Pressing the Enter key on a Form with a focused button invokes the Form.ProcessCmdKey method:
This method is called during message preprocessing to handle command keys. Command keys are keys that always take precedence over regular input keys. Examples of command keys include accelerators and menu shortcuts.
You can override this method to mark the key as handled:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// if enter pressed, return 'true' to skip default handler
if ((keyData & Keys.Return) == Keys.Return)
return true;
return base.ProcessCmdKey(ref msg, keyData);
}
If you only want to ignore Enter when buttons are focused, you can use something like:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// if a button is focused AND enter pressed, skip default handler
if (this.ActiveControl is Button && (keyData & Keys.Return) == Keys.Return)
return true;
return base.ProcessCmdKey(ref msg, keyData);
}
This could be happening if your button is set as the AcceptButton property on the form. If that is the case, just clear that property.
In addition to the other answers You can add a KeyDown event handler to your form, in its KeyEventArgs there is a SuppressKeyPress property which will prevent your Keypress from being sent to the parent control.
also see this SO question for what the differences are between handled and SuppressKeyPress,
From the SuppressKeyPress Link:
Gets or sets a value indicating whether the key event should be passed on to the underlying control.
example:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter )
e.SuppressKeyPress = true;
}

Categories