I have a Janus GridEx on cell updating I am checking for some condition and raise a message if the condition is met, what happens is that if the user pressed enter using the keyboard then the dialog will close and the enter press is passed to the
grid ,so the message will show again since on enter press the cells of the grid will be updated, this situation will keep looping until I press the OK button of the dialog form by mouse button.
How can I stop the event pressed on the dialog box to be passed again to the grid?
You may mark the event as handled to prevent propagation. Example code:
private void OnKeyPressed(Object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true; // <--
}
}
Related
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);
}
}
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?
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();
}
}
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.
I am writing a C# barcode application. I have a EAN-13 regex to detect barcodes in "Form1_KeyPress" function. I have no mechanism to detect where the input comes from. Here is my problem:
I have a reset button in the form which clears all fields and barcodes listed in a dataGridView. When I clicked on it, it gets focus as normal. When it has focus, if I read a barcode via barcode scanner, the newline at the end of each barcode reading causes this button to be clicked thus clearing all fields. So barcodes read are added to dataGridView but immediately deleted due to activation of reset button.
My current solution is to focus on a read-only textbox at the end of each "button_Click" function, but I don't want to write an irrelevant line at the end of each "click" function of buttons. What do you recommend? (by the way I cannot prevent surpress enter key in form's keydown function)
You can't capture the Enter key in the form's keystroke events because it is handled by the button.
If you add:
private void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.IsInputKey = true;
}
}
to a button then the Enter key won't cause the button to be clicked, and you will see a Form_KeyDown event for it.
You don't want to add this to every button, so create a simple UserControl which is just a button with this code added.
Update
This doesn't work for the space bar. If you set form.KeyPreview = true and add:
private void form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
e.Handled = true;
}
}
then the space bar won't press the button but it will still work in text boxes.
I don't know why Space and Enter behave differently.