Yoda
Yoda

Reputation: 18068

ArgumentException when inserting a Form in WindowsFormsHost

In WPF app I have created WindowsFormsHost and wanted to insert in it a Form containing COM/Activex control. But then I get:

A first chance exception of type 'System.ArgumentException' occurred in WindowsFormsIntegration.dll
   at System.Windows.Forms.Integration.WindowsFormsHost.set_Child(Control value)
   at HomeSecurity.MainWindow..ctor() w c:\Users\R\Documents\Visual Studio 2013\Projects\HomeSecurity\HomeSecurity\MainWindow.xaml.cs:row 26
'HomeSecurity.vshost.exe' (CLR v4.0.30319: HomeSecurity.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. Symbols loaded.

This is the contstructor of MainWindow. Class VideoStream extends class Form

      public MainWindow() {
           InitializeComponent();
           VideoStream VideoStream = new VideoStream();//creating a Form containing Activex control
           WindowsFormsHost Host = new WindowsFormsHost();//creating a host
           try {
               Host.Child = VideoStream;//setting content of Host, CAUSES THE EXCEPTION PRINTED ABOVE
           }catch(Exception e){
               Console.WriteLine(e.StackTrace);//output above
           }
        }

I cannot deal with it for long time and I have no more time. How to fix this problem?

Upvotes: 2

Views: 3716

Answers (2)

noseratio
noseratio

Reputation: 61726

Why not derive VideoStream from UserControl rather than from Form? IMO, that'd be more appropriate for re-using and hosting it inside a WPF app, and the problem would have been gone.

[EDITED] You should first add the WindowsFormsHost control to the WPF window, then add your UserControl-derived VideoStream control to the WindowsFormsHost control:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;

namespace WpfWinformsTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // add a grid first (optional)
            var grid = new Grid();
            this.Content = grid;

            // create and add a WinForms host
            WindowsFormsHost host = new WindowsFormsHost(); 
            grid.Children.Add(host);

            // add a WinForms user control
            var videoStream = new VideoStream();
            host.Child = videoStream;
        }
    }
}

You can do the same with XAML:

<Window x:Class="WpfWinformsTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:local="clr-namespace:WpfWinformsTest" 
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <WindowsFormsHost Name="wfHost" Focusable="True" Margin="10,10,10,10">
            <local:VideoStream x:Name="videoStream" />
        </WindowsFormsHost>
    </Grid>
</Window>

Here's how VideoStream may look like (of course, you can use VS Designer to add controls like axVideoPlayer to it, rather than doing it manually):

using System.Windows.Forms;

namespace WpfWinformsTest
{
    public partial class VideoStream : UserControl
    {
        AxWMPLib.AxWindowsMediaPlayer axVideoPlayer;

        public VideoStream()
        {
            InitializeComponent();

            this.axVideoPlayer = new AxWMPLib.AxWindowsMediaPlayer();
            this.axVideoPlayer.Size = new System.Drawing.Size(200, 100);
            this.Controls.Add(this.axVideoPlayer);
        }
    }
}

I suggest you read the following articles:

Walkthrough: Hosting a Windows Forms Composite Control in WPF

Walkthrough: Hosting an ActiveX Control in WPF

Upvotes: 2

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73492

Well, You can't add a Form(TopLevelControl) inside it. You'll have to make it child control first. Setting TopLevel is set to false should make it work.

VideoStream.TopLevel = false;

Note: Not only with WindowsFormsHost, even with Winforms application also you can't add a Form inside a Form unless TopLevel is set to false.

Upvotes: 16

Related Questions