Search code examples
c#wpfwinformsalt-key

Alt keys and tab don't work in Windows Forms opened from a WPF application


I have lots of old Windows Forms applications that will eventually be ported to WPF (it is a large application so it can't be done in one sprint), and I have started the process by creating a main menu in WPF. The Windows Forms applications are separate windows opened from this menu.

The Windows Forms applications are opening and working without any problems except the issues I am having with the shortcut and Tab keys. The tab key is not moving focus to the next control, and the Alt key to trigger the &Search button no longer works.

What am I doing wrong?


Solution

  • I finally managed to fix the issue by hosting the winform inside a WindowsFormsHost control inside a WPF form.

    public partial class MyWindow : Window
    {
        public MyWindow()
        {
            InitializeComponent();
    
            Form winform = new Form();
            // to embed a winform using windowsFormsHost, you need to explicitly
            // tell the form it is not the top level control or you will get
            // a runtime error.
            winform.TopLevel = false;
    
            // hide border because it will already have the WPF window border
            winform.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.windowsFormsHost.Child = winform;
        }
    
    }
    

    Please note that you may also need to hook up the winform close event if you have a button to close the form.