Form keydown in C# - c#

I use these codes to inform users when they press a button and show them in a message box that you have pressed a button. In the winform I have a button, so these codes won't working until I change button1 tabstop to false. and when I click on the button after that it won't tell users that they're pressing a button.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MessageBox.Show("You have pressed A");
}
}
These codes won't working until I change tabstop of the button to false. Help to how can I do that and let the users know when they pressed a button in any time.
Any help will be appreciated

Set the KeyPreview property of form to true from designer. It will work

Related

Assign a Keyboard key to a button click WINFORM

I am developing a small application with some buttons and textbox. What I am having problem is assigning a keyboard key (e.g. F3) to a button click.
For example if the user click the button Cash the code I wanted it's executed fine, but I want to make more easier instead clicking the button with mouse, I want the user be able to press the key on keyboard. I used the keydown event, also keypress event of that button, but still nothing.
I tried this keydown event
if (e.KeyCode == Keys.Enter)
{
btncash.PerformClick()
}
But still nothing
Do not use F3 function button it's used by OS for activating search. Enter key is fairs click event on focused control so do not use this also. Implement as suggested below.
In your Main form
Set KeyPreview to True in form load event.
Add KeyDown event handler with the following code
private void Form1_KeyDown(object sender,
KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.H)
{
btncash.PerformClick();
//btncash_Click(null, null);
}
}

Excluding autocompleting textbox from form accept property?

I have a form in which I've linked the accept property to a button called submit on the form so that whenever enter key is pressed on any control in the form it fires the submit button's click event. I also have an auto completing textbox (to/from) on the form with auto complete mode set to SuggestAppend. I want to move to the next control(textbox amount) when the user presses the enter key. I tried the textbox's keyup event like this:
private void tb_to_from_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Return)
{
tb_amount.Focus();
}
}
This moves the control to the other textbox, however, the submit button is also pressed. How do I exclude tb_to_from from the accept property of the form so that it just moves to the next control?

Programmatically change AcceptButton property and trigger ButtonClickEvent inside a KeyPress event

I have a page with two independent forms; users would only ever fill out and submit one or the other. When the user presses enter, I would like to trigger the click event of the button of the form that currently has focus. How can I accomplish this?
Trigger Button click using .PerformClick() method on KeyPress event of the TextBox after verifying whether pressed key is Enter
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check if pressed key is `Enter` key
if (e.KeyChar == (char)Keys.Return){
button1.PerformClick();
}
}

Press Enter Button To Login

Please I am working on a login form, I want it to be that once a user has entered the password and pressed the enter button keyboard, without leaving the textBox, it will just login straight away. How will I do it please?
You can try to set the AcceptButton property of your form to the login button.
Check MSDN Form.AcceptButton Property
Gets or sets the button on the form that is clicked when the user
presses the ENTER key.
You can also use a Panel like this
<asp:Panel DefaultButton="YOUR_BUTTON_ID" runat="server" ID="Panel1">
</asp:Panel>
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button.PerformClick();
}
}

Activate a button Process via custom KeyPress

Basically in a C# Windows Form Application I am working on I have 2 buttons I press button1 and button2.
How do I make it when I press 2 custom keys simultaneously(eg. CTRL+L) the program does the steps coded for button1? Keeping in mind that the window might not be active.
I have looked at this: Keypress To Simulate A Button Click in C# but I don't think this would work if the window isn't active, and its also only one button pressed not two.
In Form KeyUp Event and the Properties is KeyPressPreview = true you can achieve this task.
private void Form_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.L && e.Control)
{
yourButton.PerformClick();
e.Handled = true;
}
}
or
if you want in Keypress Event
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.Control && e.KeyCode == Keys.L)
{
yourButton.PerformClick();
e.Handled = true;
}
}
EDIT:
I realized that the OP want to the keypress Event from his statement "but I don't think this would work if the window isn't active"
You probably must check this
How to set a Windows hook in Visual C# .NET
If you precede any character in the button text with &, and keep UseMnemonic to true,. the alt+character will serve as shortcut key.
For example, in your case change the text of the buttons to button&1 and button&2 respectively. Keep UserMnemonic = true. Now if you press alt+1 then button1 will be pressed and if you press alt+2, button2 will be pressed.
Also 1 and 2 respectively will be underlined.
Hope that helps.

Categories

Resources