Winforms TextBox gets focus but doesn't capture keyboard input - wpf

This has me baffled.
I've written a popup box which consists of a WinForms UserControl hosted inside a WindowsFormsHost, which, in turn, is hosted in a Primitives.Popup which is displayed on the screen. The whole application is WPF, but this control was lifted from an earlier application written in WinForms.
The popup is activated by an external event (an incoming phone call from a CTI Server).
Inside the UserControl is a textbox control. When the user clicks in the text box, I call the Focus method on the Popup, then call the Focus method on the textbox. The textbox gets the focus. I can be fairly sure of that because the box shows a cursor after clicking in it, and also I have a "GotFocus" event handler that prints a debugging message.
However, if there was another program active at the time the incoming event occurs, any keys that are pressed on the keyboard continue to go to that program, not to the text box. Only if the user clicks in another part of my application (i.e., part of the screen outside the popup) to make it the active program, and then clicks in the text box is the text box able to receive keyboard input.
I hope I've given enough information without overwhelming you with the myriad of details. If there's something else anyone needs to point me in the right direction, I'll be happy to provide it.

Because the WinForm TextBox is hosted, setting focus on it does not activate the hosting WPF Window. Add a line to activate the Window.
private void TextBox_Click(object sender, EventArgs e)
{
this.Activate(); //activate the Window
(sender as System.Windows.Forms.TextBox).Focus();
}

Related

Setting focus on WPF ComboBox not always working

I have a solution, that has two projects, the main one and a small shared control that's used as well. In our application, a certain feature opens this shared control in a new window. I want to set the focus to the first combobox in this control when the window opens.
In my code, on the window that loads the shared control, at the end of the _Loaded event, I set focus to this combobox. But when running the code - I still have to hit tab to have 'keyboard' focus on the box (as in, I would have to hit tab to then start typing the name of one of the items in the list).
If I set a breakpoint here, hit it, and then continue - it actually is set the way it should be. If I use a WPF inspector - IsFocused is also set.
Other things noticed:
If I hit tab (to get what I want), then tab back, it takes me to the last control on the form, not to this unknown control. This makes me believe the focus is set right, but for some reason doesn't have correct keyboard focus.
If I try to use MoveNext in code, it actually selects the next item in the window, outside of the control.
How do I properly set focus here? On another combobox in the 'main' project, just calling .Focus() worked correctly.
try to postpone the focus() after all events are handled and bindings updated with QueueUserWorkItem. Something like this :
public delegate void VoidDelegate();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Some other things to do here.
System.Threading.ThreadPool.QueueUserWorkItem
(x => this.Dispatcher.Invoke(
new VoidDelegate(SetFocus), null));
}
private void SetFocus()
{
MyControlIWantToSetFocusOn.Focus();
}

How do I find out where the focus is going in my WPF application?

I have a search screen in my WPF application. The screen is implemented as a UserControl in a TabItem of a TabControl. When the user switches to the Search tab, I want the focus to go into one particular field.
So I added a Loaded event handler to the UserControl tag in the Xaml and I called the Focus method of the control I want to have the initial focus in the Loaded event handler. This worked great until I upgraded the Telerik control library I'm using today. Now, when I switch to the Search tab, the focus is NOT in the field I want to have it, but I can't tell what control does have the focus.
The field I want to have focus already has GotFocus & LostFocus event handlers for other reasons. I remembered that in Win Forms, the LostFocus event handler arguments tell you which control is going to get the focus. So I put a breakpoint in my LostFocus handler & discovered that the arguments to the LostFocus event handler in WPF don't include that information.
How can I figure out where the focus is going without putting GotFocus handlers on every control in my UserControl?
Tony
You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.
private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
textBox1.Text = ((FrameworkElement)e.NewFocus).Name ;
}
Try to press Tab Key and see if it helps you find the control in focus.
You can also use Snoop as suggested in this Q/A: Any tips on debugging focus issues in WPF?
For starters, Snoop shows the current focused element and the current
FocusScope in the status bar.
You can get it to show you all the GotFocus and LostFocus events:
1. Run your app.
2. Run Snoop.
3. Choose your app in the dropdown.
4. Click the binoculars ("Snoop") button.
5. On the right pane, click the Events tab.
6. Click to bring down the dropdown.
7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
8. Now do what you need to do to manipulate focus and watch the Snoop window.
Similarly you can track the FocusManager events the same way.

Capture Silverlight 4 event when panel clicked

I have a small Silverlight 4 app that essentially consists of a grid containing a label and a combo box. When I click the label, I replace it with a second text box so that I can edit the label (much the way you can edit the name of a Silverlight control in VS2010).
I have a LostFocus event handler on the text box that will end editing when the control loses focus (restoring the updated label). Trouble is, users tend to click on the panel when they are done editing rather than on another control (or hitting Enter, which is also supported).
I tried adding a left mouse down event handler to the panel. However, that only fires when the text box does not have the focus (I guess the text box captured the mouse?)
Is there an approach to recognize that a non-input control was clicked that would enable me to terminate edit mode?
You can subscribe to Grid's MouseLeftButtonDown routed event using the following code:
panel.AddHandler(UIElement.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(panel_OnMouseLeftButtonDown), true);
Unlike common events routed events are bubbled from innermost control to its parent, then to grandparent etc. In the same way you could subscribe to panel's parent to intercept clicks outside your panel.

Clicking on Popup outside bounds of TaskPane loses keyboard focus

I'm developing a Microsoft Word TaskPane containing a WPF ElementHost. The hosted WPF uses a Popup that is intended to behave as a ComboBox's does. I believe you can reproduce my problem like this:
popup.IsOpen = true;
popup.StaysOpen = false;
Mouse.Capture(ancestor_of_popup, CaptureMode.SubTree);
When a region of the Popup lies outside the bounds of the TaskPane, click the mouse in that region. The entire TaskPane loses keyboard focus to Word's main window, but retains mouse capture. At this point, you can use the Popup with a mouse while typing on the keyboard edits your document!
If the TaskPane loses keyboard focus then the Popup should close, but then any control in the region of a Popup that lies outside the bounds of the TaskPane becomes unusable. Any ideas?
This looks like a similar problem to Popups in XBAP stop receiving anything but mouse events after losing focus?.
Apparently there is a known bug with focus in WPF popup controls, although that may be specific to XBAP (you didn't specify if this was a WPF desktop or browser application).

WPF input gesture

I am working a WPF application, where I have maintained a Menu Bar with Input Gestures i.e keyboard Shortcuts.
For Save As menu item, I have kept Ctrl+A as per User's requirement. It works fine as far as the focus is on the main window.
Now my problem is, suppose use has navigated in some Listbox in window, and if he presses Ctrl+A, then Select All functionality takes places for the list box and Save As dialog box does not get called (as i have done the command binding for this input gesture)
Any idea how can I avoid this? and yes, I can not change my input gesture. It has to be Ctrl+A. :)
Thanks
I think you could change the command bindings on the list box object to remove the binding for the command. Look at the ListBox.CommandBindings list.
You could also turn off Focusable on the ListBox so that it never receives keyboard commands.
You could also check out the eventing model. You could probably catch the keydown as the preview events "bubble up" from the root of the logical tree and then they are passed down from the end element down. They can be marked as handled on the way up or the way back down.

Resources