How to change button click event into enter event? - c#

I'm new on C#, I made a button which makes appear another button and I want to remove the click event of this new one and change it into an enter(key) event.
Code1
Code2
When the button appears it only works when I click it but no when I press Enter.

As to your question, https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.enter?view=net-5.0
The enter event is for when keyboard or mouse enter the control (button in this case).
From the sounds of your question, you are wanting to click the button when the 'Enter' key is pressed.
If this is WinForms, then you might actually want a property on the parent form called 'AcceptButton' (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.acceptbutton?view=net-5.0). This will make it so anytime the Enter key is hit (with the exception of controls such as a RichTextBox that accepts the return key) that button will be clicked (course you still need the button click event for the button).
Although you seem to be doing something weird in your example code image, so I'm not sure that's the correct solution for it. Instead, you need to look at maybe the Keyboard events, such as KeyPress (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.keypress?view=net-5.0). There are several: KeyDown, KeyPress, KeyUp, and PreviewKeyDown.
Note these events require Keyboard focus on the control in question. So if add them to the button you are creating in your example code, then that button will need keyboard focus to receive the keyboard events.
Also, just FYI, if a button has keyboard focus, by default the SpaceBar will click that button for you, while the Enter key is generally reserved for clicking the Form's AcceptButton.

Related

How to remove focus from a button

I have a panel on which there is a button.But there is a problem. I need to use KeyEventHandler to catch the Enter press, the button is pressing, and the event is not processed. I tried artificially giving focus to Form, but it didn't help.If I don't add buttons, everything works fine. Can you tell me how to solve this?
I created a new project and tried: with the button, the Event is not called, and the button is pressed, without the button, the event is called.
you need to enable KeyPreview property of that form, to catch a KeyEventHandler. Also check that your AcceptButton property has correct value

Omit action when pressing Enter on button

Is there a way to omit the (whatever) performed action on a button when the key is pressed? I already got some good inputs from SO and tried to catch the event via OnKeyPress or OnKeyDown - but I could not omit the Enter-action. I also liked the idea of disabling the Tabstop property, but as soon as I click the button the problem reoccurs.
OnKeyDown only seems to fire when "casual" keys (like a or z) are pressed, but not when Enter is hit. How can I detect it - when Enter is hit - on the button itself?
When you press ENTER inside a non-multiline field on a form with a default button set, the click event for this button is fired, like a click, not like a keyevent (keypress, keyup, keydown, etc).
Form.AcceptButton
Gets or sets the button on the form that is clicked when the user
presses the ENTER key.
If you don't want this behaviour (or want to prevent it), maybe you should remove it as the default button.
But if you need it to be default only in certain situations, you can bind/unbind it programatically:
form1.AcceptButton = btnSave;
form1.AcceptButton = null;
You can use : if(e.KeyCode == Keys.Enter){}
Check the Button.IsDefault property
Well, the fact that
When you press ENTER [...] the click event for this button is fired
made me think about the use of the OnMouseUp(MouseEventArgs e) method instead of OnClick(EventArgs e). I gave it a shot and it totally fits my requirements - plus: it gives me the opportunity to only exclude the wrapped buttons from said issue, not involving any other controls in my application (like Form.AcceptButton).
Thanks for all your input, which made me think about the issue from another perspective!

Virtual keyboard in my form. Preventing button focus

I'm trying to create a virtual NumPad in my form. When the user clicks any of the numpad buttons, the corresponding number should be sent to the currently focused TextBox (or any other text control).
Simulating keystrokes aside, how can I prevent the numpad button, which is inside a numpad control, from stealing focus from the already focused TextBox?
I've tried googling and searching SO but didn't find anything that helped me.
Add this line in click event of Numpad button.
this.ActiveControl = YourTextboxID;
SetStyle(ControlStyles.Selectable, false);

Control's Focused property has changed as of Leave handler when using mouse, but not keyboard

I am working with a TextBox, and need to fire some logic when the textbox has lost focus.
My problem is twofold:
The Leave event is firing on every keypress for some reason, meaning the logic is run with every keypress when it should not.
When using the Focused property of the Textbox as a double-check, simply exiting out if the property is still set, it now works when the user uses the mouse to leave, but not when the user tabs out.
The Focused property of the TextBox in question is False as of when its Leave event fires when the mouse is used to change focus, but it is still True when the Leave event fires due to a Tab keypress. Seriously?
I need a workaround, because the logic firing on every keystroke is causing a problem for users right now that needs to be fixed post-haste.
I created a form with a textbox on it and attached event handler to the leave event of that text box. I then typed a bunch of stuff into said textbox. The event was not raised. I hit tab, the event was raised. I then clicked back in the textbox, typed some more, and then clicked another control and the event was raised.
I'm just saying that something else is interfering with the textbox. I would look into that a little more, or post some code demonstrating the problem.

In Gtk, can I make a Button's mnemonic not require an "alt" mask?

I have 6 Buttons, labeled "_0" through "_5". I would like for each button to be pressed when the user presses the corresponding number key. Right now, they must press Alt + the corresponding number key.
I can sort of work around this by giving each button an Accelerator, but it's not quite the same thing. With accelerators, as soon as the key is pressed, the button's Clicked signal is triggered. With mnemonics, the button becomes depressed when the key is pressed, and the Clicked signal isn't triggered until the button is released. I prefer this, because it helps the user to see what is happening.
Is there any way I can get the behavior of Mnemonics, but without requiring the Alt key?
You can have the gtk window catch key events with the event mask settings in the window class. I can't be much more specific than that with callbacks and key types, because I use GTKmm (C++ bindings) but the approach should be similar. Basically when you catch the required key event in your window you can call the button press in code. The window has be to selected (in focus) however.

Categories

Resources