WindowsFormsHost and keyboard focus
-
I haven't tried to embed a WinForm control in a WPF application until now, so I may not be doing this right. When I create the WindowsFormsHost and add the WinForm control (the TextEditorControl from SharpDevelop) I cannot programmatically assign the keyboard focus to the WinForm control. First I tried using the Focus() method on the control itself and on its text area. I never see the cursor blinking and keyboard input is ignored. Then I used interop to pull in the user32.dll function, SetFocus(HWND) which when called with the control's text area Handle makes the cursor blink in the control, but keyboard input is still ignored. I created a subclass of WindowsFormsHost so that I could set some breakpoints on the various On*Focus methods. The only thing that I can see is that OnLostKeyboardFocus is called when I manually click on the text editor control, but the KeyboardFocusChangedEventArgs.NewFocus argument is null, which means the new control is not recognized by WPF. That makes perfect sense since the control receiving the keyboard focus is a WinForms control. So, any thoughts? Has anyone ever encounter this problem before? I have been searching on Google for hours and have not come up with anything. Thanks!
Paul
-
I haven't tried to embed a WinForm control in a WPF application until now, so I may not be doing this right. When I create the WindowsFormsHost and add the WinForm control (the TextEditorControl from SharpDevelop) I cannot programmatically assign the keyboard focus to the WinForm control. First I tried using the Focus() method on the control itself and on its text area. I never see the cursor blinking and keyboard input is ignored. Then I used interop to pull in the user32.dll function, SetFocus(HWND) which when called with the control's text area Handle makes the cursor blink in the control, but keyboard input is still ignored. I created a subclass of WindowsFormsHost so that I could set some breakpoints on the various On*Focus methods. The only thing that I can see is that OnLostKeyboardFocus is called when I manually click on the text editor control, but the KeyboardFocusChangedEventArgs.NewFocus argument is null, which means the new control is not recognized by WPF. That makes perfect sense since the control receiving the keyboard focus is a WinForms control. So, any thoughts? Has anyone ever encounter this problem before? I have been searching on Google for hours and have not come up with anything. Thanks!
Paul
Here's a little more information: If I create a subclass of
WindowsFormsHost
and override theOnKeyboardGotFocus
method like this:[DllImport( "user32.dll" )]
private static extern IntPtr SetFocus( IntPtr hWnd );protected override void OnGotKeyboardFocus( KeyboardFocusChangedEventArgs e )
{
base.OnGotKeyboardFocus( e );
SetFocus( editor.ActiveTextAreaControl.TextArea.Handle );
}I can direct the keyboard focus to the editor control, but the cursor never appears. If I override
OnGotFocus
in the same way, I can see the cursor blinking but the keyboard focus doesn't change. If I override both, I get the keyboard focus but no cursor.Paul
-
I haven't tried to embed a WinForm control in a WPF application until now, so I may not be doing this right. When I create the WindowsFormsHost and add the WinForm control (the TextEditorControl from SharpDevelop) I cannot programmatically assign the keyboard focus to the WinForm control. First I tried using the Focus() method on the control itself and on its text area. I never see the cursor blinking and keyboard input is ignored. Then I used interop to pull in the user32.dll function, SetFocus(HWND) which when called with the control's text area Handle makes the cursor blink in the control, but keyboard input is still ignored. I created a subclass of WindowsFormsHost so that I could set some breakpoints on the various On*Focus methods. The only thing that I can see is that OnLostKeyboardFocus is called when I manually click on the text editor control, but the KeyboardFocusChangedEventArgs.NewFocus argument is null, which means the new control is not recognized by WPF. That makes perfect sense since the control receiving the keyboard focus is a WinForms control. So, any thoughts? Has anyone ever encounter this problem before? I have been searching on Google for hours and have not come up with anything. Thanks!
Paul
If you can give me a small ready-to-run sample I might be able to help you. We have lots of Winforms/Wpf interaction and I remember that it was quite a challenge to get the focus right. You might want to try to set the focus to your element via a dispatcher like so:
Dispatcher.Invoke(new Action(() => Focus()), System.Windows.Threading.DispatcherPriority.Background);
this might also help: http://geekswithblogs.net/rakker/archive/2007/07/27/114232.aspx[^]Listen to the toad! www.dotnettoad.com[^]