WPF Keep textbox focussed while pressing tab key - c#

I am writing a WPF application where we are supposed to shift focus from one button to another by pressing the TAB key.
All is fine, but for a specific page, there is a textbox which is always focused so that the user can type in it immediately. But now, when I press TAB, the focus is shifted to the button (which is as per requirement) but as the focus is lost from the textbox, I cannot type anymore.
I want to have a behavior where I can keep the Keyboard focus on textbox but should be able to control the buttons using TAB key as well. In a way, I need to have focus on textbox and the buttons at the same time. Is this possible?
Tried the FocusManager.IsFocusScope = "true", but this did not help.

Related

Need to tap twice on touchscreen to click button in WPF

I have a WPF application that at one point brings up another window where the user can enter text in a field that is selected on open then click OK to save the text. This is working correctly on my desktop using a mouse, but when I run the application on a tablet (Surface Pro) the OK button needs to be tapped twice to save the text. The first tap highlights the button, then the second tap clicks it. Is there any way to allow the user to click the button with just one tap on the screen?
This only happens when the application switches to the new window. The main window only requires one tap to click buttons (Though I have noticed that they require two when switching back to the main window). It seems like this issue has to do with focus or something because if I tap somewhere on the new window before clicking OK, I can tap OK once and it will trigger the click event.
You probably need something like this in your code:
textBox1.Focus();
The other place to consider is the Tab order of the items on the form. Once the focus leaves the textbox, it moves to the next highest tab order object. It should be the OK button.
You can adjust the Tab order by looking at the properties of the objects on the form.
This is a bug in WPF combined with a touch display.
Because the textbox is focused, and you press the button, the textbox gets unfocused and the button gets focus. When the button has focus, you just have to press it in order to save your text.
There really isn't a thing you can do about it, since the touchscreen focuses first on the button before you can fire the event (I think it probaly is a kind of a safety feature).

On Wpf container tabbing is not loosing focus on button

A button on wpf container is not loosing focus style while tabbing across the windows.The control does go to the other elements but the button will still have the focus style on it.
Does the button have IsDefault=True? This will cause the button to always have keyboard focus for the Enter key as long as another control is not capturing the keyboard input.

Radio buttons Navigation by KeyBoard

I have 3 radio buttons on my WinForm.
I would like to give the user the option to navigate between them also by keyboard.
Is there a way to enable it?
I understand I have to use this code:
if (e.KeyChar==Convert.ToChar(Keys.Down))
But how do I know which single radio button I have to set as checked?
The simplest solution is to use keyboard shortcuts. This entails prefixing one character in each RadioButton's text with the & character.
For example if the text of your radio button is "&Big option" then the user can select this option by pressing the [ALT] and B keys at the same time.
Additionally, once one of the radio buttons has the focus you can navigate between them by using the up and down arrows. In general, the user can navigate between controls by using the [Tab] key. In VS 2010 the tab order may be modified by selecting the View->Tab Order menu item.
The same keyboard shortcut trick works for many other controls. For example if you have a TextBox control preceded by a label control you can prefix a letter in the Label control with &. Now since the Label (by default) can't take the focus, when the user uses the Label's keyboard shortcut, the focus will move to the next control in the Tab Order i.e. the TextBox.
If you want a control to be skipped when using the [Tab] key set its TabStop property to False.

Buttons Are Automatically Selected (how to turn this off?)

I have winform buttons that when you load the form, a certain button is selected. What I mean by selected, is that if "enter" is pressed, the button is pressed.
How can I change my buttons so they don't do this anymore?
Your tab order is set in the order in which you add controls on the form. If your first control which can be pressed/selected/edited is that button which is getting pressed, the focus will automatically be on it when form is loaded.
You can cheat by setting the focus to some other control (maybe which is not visible? !hint * hint!) to avoid the button to be selected at first.
But also make sure tht button is not the AcceptButton of the form.
Two concepts have been touched on by Nayan and rerun:
1) AcceptButton
2) Tab order
There is one more I would add and then try to explain how the three things relate:
3) Focus
Focus means that a child control has the "keyboard focus". When a control has focus, it receives keyboard input and can respond to it. Focus is changed either by clicking a control with the mouse, or by using the Tab key.
Tab order is the order in which controls receive focus when the Tab key is pressed. It also determains which control initially gets focus (the first one in tab order).
The AcceptButton concept is a bit of a hybred. If a form's AcceptButton property is set to a button control, that button is pressed when the user presses the Enter key while focus is on any control that does not process the enter key itself. Typically the 'Ok' button on a form is set as the AcceptButton so that the user can enter data and press Enter as a shortcut to pressing the Ok button.
You need to set the acceptbutton on the form.
Element.Select() is what worked for me.

wpf textbox focus issue

I have a control with several textbox controls on it.
Now, when I press tab after I edit one of the textboxes, the focus is switched to the next textbox BUT I need to press an additional tab in order for it to enter the "editing" stage.
The first tab simply draws a dotted background on the textbox ... and the second one actually puts the cursor position inside the textbox. Is there a way when I press tab, to automatically set the cursor inside the textbox?
Thanks./
Are you sure you don't have any hidden controls or other controls that might be accepting focus before your textbox? Either way, you should be able to update the tabindex value of your controls so they follow a logical sequence when tab is pressed.

Categories