Virtual keyboard in my form. Preventing button focus - c#

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

Related

How to change button click event into enter event?

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.

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!

TextBox control dont show virtual keyboard after setting focus by code second time

I have problem with Textbox control, more specifficaly with focus and virtual keyboard. I have view with few TextBoxs, I set focus in TextBox with this code: TextBox.Focus(FocusState.Programmatic); and everything is fine. TextBox gets focus, virtual keyboard shows. Then I hide Keyboard with button from virtual keyboard. Then I try to give focus to other TextBox in the same way. TextBox2.Focus(FocusState.Programmatic); and TextBox2 gets focus but virtual keyboard don't show. When I click this TextBox virtual keyboard shows, but TextBox.Focus(FocusState.Programmatic); don't work anymore after i use hide keyboard button form virtual keyboard.
I tried this on blank view without other controls. I have to set focus by code in my project. And this issue is big problem for me. Does anybody know solution for this problem?

confirm messagebox only with mouse

I wrote an application, where the user filled in a form with some text and pressed enter when a textbox is filled. After this some checks were done and a nominal MessageBos with an OK-Button appeared. Some useres didn't read the messagebox, typed in as there were in the next textbox, pressed the OK-Button and the warning/error-message disappeared without reading.
I know, I cannot to control, that the user really read the message, but I want, that the usere use the mouse for conforming. Is there a way, that the Ok-button doesn't have the focus when the messagebox is shown ? Or do I have to write my Own Messagebox with an extra field, that has the focus.
While protecting users from themselves is typically a no-win endeavor, I would suggest that to do what you want would require you to construct a form of your own in which you could capture the keyboard input and disallow the dismissal of the dialog when the Enter key is pressed.

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