Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WindowsFormsHost and keyboard focus

WindowsFormsHost and keyboard focus

Scheduled Pinned Locked Moved WPF
csharpwpfwinformscomalgorithms
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul A Howes
    wrote on last edited by
    #1

    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

    P P 2 Replies Last reply
    0
    • P Paul A Howes

      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

      P Offline
      P Offline
      Paul A Howes
      wrote on last edited by
      #2

      Here's a little more information: If I create a subclass of WindowsFormsHost and override the OnKeyboardGotFocus 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

      1 Reply Last reply
      0
      • P Paul A Howes

        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

        P Offline
        P Offline
        Patrick Klug
        wrote on last edited by
        #3

        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[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups