{"id":377,"date":"2011-06-22T08:27:27","date_gmt":"2011-06-22T06:27:27","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=377"},"modified":"2011-06-22T08:27:27","modified_gmt":"2011-06-22T06:27:27","slug":"embedding-a-form-inside-another-control","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2011\/06\/embedding-a-form-inside-another-control\/","title":{"rendered":"Embedding a Form inside another control"},"content":{"rendered":"<p>This is something I was thinking about at the beginning of my TDI (Tabbed document interface) &#8216;adventure&#8217; but I didn&#8217;t think it would be easy or possible back then. Then I came across a custom tab control (<a title=\"A highly configurable MDI tab control from scratch\" href=\"http:\/\/www.codeproject.com\/KB\/tabs\/MDITabControl.aspx\" target=\"_blank\">A highly configurable MDI tab control from scratch<\/a>) that actually allowed you to use plain old WinForms to add to it. The control itself was written in VB and ultimately I abandoned using it but I was intrigued with how it could use a plain WinForm. It actually has some real advantages like.<\/p>\n<ul>\n<li>A form you can design easier in the IDE (than a user control).<\/li>\n<li>A form can be tested\/used separately on it own without being embedded onto something else first.<\/li>\n<li>And I had a blank thinking of more reasons&#8230;<\/li>\n<\/ul>\n<p>If you try to just add a Form to another container &#8211; like a panel, tabpage etc. and try to run it you would get the error: &#8216;<em>Top-level control cannot be added to a control.<\/em>&#8216;. This happens because Forms are marked as Top Level controls. Fortunately, there is a simple way around it. You need to mark the form as not being a &#8216;Top level control&#8217; like this: (assuming you have a panel called panel1)<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">Form <\/span>f = <span style=\"color: #0000ff;\">new Form<\/span>();<br \/>\nf.TopLevel = <span style=\"color: #0000ff;\">false<\/span>;<br \/>\npanel1.Controls.Add(f);<br \/>\nf.Show(); <span style=\"color: #008000;\">\/\/this is needed to actually have the form display itself inside the container<\/span><\/p><\/blockquote>\n<p>That will make the form load inside the panel without run-time errors &#8211; but it would look ugly if you just leave it like that. To properly &#8217;embed&#8217; it you need to add some more code:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">Form <\/span>f = <span style=\"color: #0000ff;\">new Form<\/span>();<br \/>\nf.TopLevel = <span style=\"color: #0000ff;\">false<\/span>;<br \/>\nf.MdiParent = <span style=\"color: #0000ff;\">null<\/span>;<br \/>\nf.FormBorderStyle = System.Windows.Forms.<span style=\"color: #0000ff;\">FormBorderStyle<\/span>.None;<br \/>\nf.Dock = <span style=\"color: #0000ff;\">DockStyle<\/span>.Fill;<br \/>\npanel1.Controls.Add(f);<br \/>\nf.Show();<\/p><\/blockquote>\n<p>So, there you have it!<\/p>\n<div id=\"_mcePaste\" class=\"mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">\n<pre style=\"font-family: Courier New; font-size: 19; color: #e8e8e8; background: #2d2d2d;\"><span style=\"color: #00d2d2;\">Form2<\/span>\u00a0<span style=\"color: #d0d0d0;\">f<\/span>\u00a0<span style=\"color: silver;\">=<\/span>\u00a0<span style=\"color: salmon;\">new<\/span>\u00a0<span style=\"color: #00d2d2;\">Form2<\/span>();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #d0d0d0;\">f<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: #d0d0d0;\">TopLevel<\/span>\u00a0<span style=\"color: silver;\">=<\/span>\u00a0<span style=\"color: salmon;\">false<\/span>;<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is something I was thinking about at the beginning of my TDI (Tabbed document interface) &#8216;adventure&#8217; but I didn&#8217;t think it would be easy or possible back then. Then I came across a custom tab control (A highly configurable &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2011\/06\/embedding-a-form-inside-another-control\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,303,20,10],"class_list":["post-377","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-development","tag-mdi","tag-winforms"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":1,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions\/378"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}