{"id":114,"date":"2007-08-19T22:15:00","date_gmt":"2007-08-19T22:15:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2007\/08\/19\/merging-menu-and-toolbar-to-mdi-form\/"},"modified":"2007-08-19T22:15:00","modified_gmt":"2007-08-19T22:15:00","slug":"merging-menu-and-toolbar-to-mdi-form","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2007\/08\/19\/merging-menu-and-toolbar-to-mdi-form\/","title":{"rendered":"Merging Menu and Toolbar to MDI Form"},"content":{"rendered":"<p>I have a form that it is used both stand alone and within an MDI from. When used in an MDI form I wanted to move the menu&#8217;s and toolbars to the shared placed on the MDI form whereas using it on a standalone case I had no option than having it on the owner form.<\/p>\n<p>To establish this behavior I had to implement the activate and deactivate methods to make the toolstrip and menu strip, which hold the toolbar items and menu items, invisible and move the items to the new container. This is a stait forward solution as illustrated below:<\/p>\n<p><\/p>\n<pre><br \/><span style=\"color:#0000ff;\">private<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">void<\/span><span style=\"color:#000000;\"> OnFormActivate(<\/span><span style=\"color:#0000ff;\">object<\/span><span style=\"color:#000000;\"> sender, EventArgs e)<br \/>{<br \/>    <\/span><span style=\"color:#0000ff;\">try<\/span><span style=\"color:#000000;\"><br \/>    {<br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Get the parent<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        Form parent <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.MdiParent;<br \/>        <\/span><span style=\"color:#0000ff;\">if<\/span><span style=\"color:#000000;\"> (parent <\/span><span style=\"color:#000000;\">==<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">null<\/span><span style=\"color:#000000;\">)<br \/>            <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\">;<br \/>   <br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Get the toolbar on the parent by name<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        <\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> targetToolbar <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">toolStrip<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">; <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> using the name of the control<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        <\/span><span style=\"color:#0000ff;\">if<\/span><span style=\"color:#000000;\"> (<\/span><span style=\"color:#000000;\">!<\/span><span style=\"color:#000000;\">parent.Controls.ContainsKey(targetToolbar))<br \/>            <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\">;        <br \/>        ToolStrip target <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> parent.Controls[targetToolbar] <\/span><span style=\"color:#0000ff;\">as<\/span><span style=\"color:#000000;\"> ToolStrip;<br \/>        <\/span><span style=\"color:#0000ff;\">if<\/span><span style=\"color:#000000;\"> (target <\/span><span style=\"color:#000000;\">==<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">null<\/span><span style=\"color:#000000;\">)<br \/>            <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\">;<br \/>   <br \/>        ToolStripManager.Merge(<\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.toolStrip1, target);<br \/>   <br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Hide the original container<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.toolStrip1.Visible <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">false<\/span><span style=\"color:#000000;\">;<br \/>        <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.menuStrip1.Visible <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">false<\/span><span style=\"color:#000000;\">;<br \/>        SetToolbarCaption();<br \/>    }<br \/>    <\/span><span style=\"color:#0000ff;\">catch<\/span><span style=\"color:#000000;\"> { } <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Any exception will cause the toolbar to remain on its oroginal parent<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">}<br \/><\/span><span style=\"color:#0000ff;\">private<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">void<\/span><span style=\"color:#000000;\"> OnFormDeactivate(<\/span><span style=\"color:#0000ff;\">object<\/span><span style=\"color:#000000;\"> sender, EventArgs e)<br \/>{<br \/>    <\/span><span style=\"color:#0000ff;\">try<\/span><span style=\"color:#000000;\"><br \/>    {<br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Get the parent<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        Form parent <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.MdiParent;<br \/>        <\/span><span style=\"color:#0000ff;\">if<\/span><span style=\"color:#000000;\"> (parent <\/span><span style=\"color:#000000;\">==<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">null<\/span><span style=\"color:#000000;\">)<br \/>            <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\">;<br \/><br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Get the toolbar on the parent by name<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        <\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> targetToolbar <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">toolStrip<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">;<br \/>        <\/span><span style=\"color:#0000ff;\">if<\/span><span style=\"color:#000000;\"> (<\/span><span style=\"color:#000000;\">!<\/span><span style=\"color:#000000;\">parent.Controls.ContainsKey(targetToolbar))<br \/>            <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\">;<br \/><br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Double check<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        ToolStrip target <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> parent.Controls[targetToolbar] <\/span><span style=\"color:#0000ff;\">as<\/span><span style=\"color:#000000;\"> ToolStrip;<br \/>        <\/span><span style=\"color:#0000ff;\">if<\/span><span style=\"color:#000000;\"> (target <\/span><span style=\"color:#000000;\">==<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">null<\/span><span style=\"color:#000000;\">)<br \/>            <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\">;<br \/><br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Actual Merge Revert of the contents of the toolStrip<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        ToolStripManager.RevertMerge(target, <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.toolStrip1);<br \/><br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Keep hiding the original container<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.toolStrip1.Visible <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">false<\/span><span style=\"color:#000000;\">;<br \/>    }<br \/>    <\/span><span style=\"color:#0000ff;\">catch<\/span><span style=\"color:#000000;\"> { }<br \/>}<\/span><\/pre>\n<p>All I need to do next is to wire these events to the propper events: FormActivate and Deactivate.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a form that it is used both stand alone and within an MDI from. When used in an MDI form I wanted to move the menu&#8217;s and toolbars to the shared placed on the MDI form whereas using it on a standalone case I had no option than having it on the owner &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2007\/08\/19\/merging-menu-and-toolbar-to-mdi-form\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Merging Menu and Toolbar to MDI Form&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[85,92,90,93,91],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":1,"uagb_excerpt":"I have a form that it is used both stand alone and within an MDI from. When used in an MDI form I wanted to move the menu&#8217;s and toolbars to the shared placed on the MDI form whereas using it on a standalone case I had no option than having it on the owner&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/114"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}