C#: Perform PictureBox.Click if pressed enter - c#

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;
}

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();
}

Windows form hot keys using ALT+Key, if the same key combination with case change

We have created windows form which has two buttons Cancel and clear.
we have give button text as &Cancel and &clear for shortcut key combination as Alt+key.
but when we press Alt+c it only works for cancel button.
Instead why it won't tab between these two buttons in Windows form, which works like tabbing in VB6 forms.
I need to tab between these two buttons if Alt+c is pressed.
Please suggest any form property or button property needs to be set to achive this?
You can try overriding the ProcessCmdKey method. The below should work if you press alt, let go of alt, and then press the c key.
private bool altPressed;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (altPressed)
{
if (keyData == Keys.C)
{
this.SelectNextControl(ActiveControl, true, true, true, true);
altPressed = false;
return true;
}
}
if (keyData == (Keys.Menu | Keys.Alt))
{
altPressed = true;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

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;
}

How to capture Key press event for a text box?

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)
{
}
}

How to Stop Window Error Sound When Typing 'Enter' or 'Esc'

I have a form with a single text box on it. No other controls. Whenever I type the 'Enter' key or the 'Esc' key, the form functions as I desire; but I hear that horrible Windows error sound. The code looks similar to the following...
public class EntryForm: Form
{
public EntryForm()
{
}
private void EntryTextBox_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
// do some stuff
Hide(); // I've also used DialogResult = DialogResult.OK here
e.Handled = true;
}
else if(e.KeyCode == Keys.Escape)
{
Hide(); // I've also used DialogResult = DialogResult.Cancel here
e.Handled = true;
}
}
}
I can 'hack' it and make the noise stop by adding the following code to the form's constructor.
AcceptButton = new Button();
CancelButton = new Button();
As I stated this causes the sound to not play, but I think this is bad form; especially since I don't need a button on the form.
Anyone know why this is the behavior and if there is a cleaner way to stop the error sound from playing?
In the KeyDown event, set e.Handled = true and e.SuppressKeyPress = true.
There's a more "correct" fix, one that works regardless of how many controls you have and follows the Windows Forms design model. Paste this code into your form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Escape || keyData == Keys.Enter) {
this.Hide();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
This is too long a reply to Nobugz answer to fit in a comment. If you use Nobugz code as is :
the Form is going to be hidden no matter which control on the form is active and has keyboard input focus, and that is independent of whether the Form has the 'KeyPreview property set to 'true or 'false.
Here's what you need to do to make only a specific control (in this case a TextBox named 'textBox1) be hidden in the ProcessCmdKeys over-ride :
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (msg.HWnd == textBox1.Handle)
{
if (keyData == Keys.Escape || keyData == Keys.Enter)
{
textBox1.Hide();
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
Of course if you wanted to handle the case of multiple controls needing to be hidden, you could implement a 'switch statement or whatever to test the msg.HWnd against : note I make the assumption here that all controls that could have keyboard input will have a valid HWnd.
Some memory (vague) of a situation in which I used this technique, and a text input control somehow still had keyboard input focus ... when I did not intend for it to ... makes me want to add an additional test like this :
&& this.ActiveControl == textBox1
But, take that with a "grain of salt" since I can't be certain it is necessary.

Categories