{"record":{"id":"1b8d13613d9ddd7c","repo":"dotnet/wpf","slug":"child-cannot-be-a-top-level-form","errorCode":null,"errorMessage":"Child cannot be a top level form.","messagePattern":"Child cannot be a top level form\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/WindowsFormsHost.cs","lineNumber":317,"sourceCode":"\n        /// <summary>\n        ///     Gets or sets the child control hosted by the WindowsFormsHost element.\n        /// </summary>\n        public Control Child\n        {\n            get\n            {\n                return _hostContainerInternal.Child;\n            }\n            set\n            {\n                Control oldChild = Child;\n                SWF.Form form = value as SWF.Form;\n                if (form != null)\n                {\n                    if (form.TopLevel)\n                    {  //WinOS #1030878 - Can't host top-level forms\n                        throw new ArgumentException(SR.Host_ChildCantBeTopLevelForm);\n                    }\n                    else\n                    {\n                        form.ControlBox = false;\n                    }\n                }\n                _hostContainerInternal.Child = value;\n                if (Child != null)\n                {\n                    _propertyMap.ApplyAll();\n\n                    Child.Margin = SWF.Padding.Empty;\n                    Child.Dock = DockStyle.None;\n                    Child.AutoSize = false;\n                    Child.Location = SD.Point.Empty;\n                    // clear the cached size\n                    _priorConstraint = new Size(double.NaN, double.NaN);\n                }","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/WindowsFormsHost.cs#L299-L335","documentation":"WindowsFormsHost cannot embed a top-level Form as its Child: top-level windows own their HWND and cannot be parented inside another control's visual tree (WinOS bug #1030878). The Child property setter throws ArgumentException (message: 'Child cannot be a top level form') when the assigned Control is a Form with TopLevel == true.","triggerScenarios":"Assigning a Form (e.g. new MyForm() { TopLevel = true }) to windowsFormsHost.Child; hosting a form created with Application.Run or a designer form without setting TopLevel = false.","commonSituations":"Migrating a legacy WinForms dialog into a WPF app by dropping the Form into a WindowsFormsHost; code reuse of a Form that was never designed for embedding.","solutions":["Set form.TopLevel = false before assigning it to windowsFormsHost.Child.","Prefer a plain UserControl/Control over a Form for embedding.","Refactor the Form's content into a UserControl and host that instead."],"exampleFix":"// before\nhost.Child = myForm; // myForm.TopLevel == true -> ArgumentException\n// after\nmyForm.TopLevel = false;\nmyForm.FormBorderStyle = FormBorderStyle.None;\nhost.Child = myForm;","handlingStrategy":"validation","validationCode":"if (control is Form f)\n{\n    f.TopLevel = false;\n    f.FormBorderStyle = FormBorderStyle.None;\n}\nhost.Child = control;","typeGuard":"bool CanEmbedInHost(System.Windows.Forms.Control c) => !(c is Form f && f.TopLevel);","tryCatchPattern":"try { host.Child = control; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"top level\"))\n{ log.Error(\"Set Form.TopLevel = false before hosting\", ex); }","preventionTips":["Always set TopLevel = false on any Form you intend to embed","Prefer UserControl over Form for content hosted in WindowsFormsHost","Centralize child assignment in a helper that normalizes forms"],"tags":["winforms","wpf-interop","form-hosting","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}