CAB: MdiWorkspace

When you download the Composite UI (CAB) source, one thing becomes apparent from the samples, there’s no MdiWorkspace sample :(

The following code extracts show how to use CAB with an MdiWorkspace.  When File/New is clicked and a new child form is created with the SmartPart (ButtonControl) embedded.  When the child form is closed, the SmartPart is disposed but the WorkItem (ButtonWorkItem) remains alive, and isn’t terminated.  It appears that it’s up to the application to decide when to terminate the WorkItem (a run-time container for components that are working together to fulfill a use case).  It’s also interesting to note that the Activate and Deactivate of the WorkItem is in sync with the current active child form.

using System; using System.Windows.Forms; using Microsoft.Practices.CompositeUI; using Microsoft.Practices.CompositeUI.WinForms; namespace MdiApp { public class ShellApp : FormShellApplication<WorkItem, Form1> { MdiWorkspace m_mdiApp; [STAThread] public static void Main() { new ShellApp().Run(); } protected override void AfterShellCreated() { base.AfterShellCreated(); ToolStripMenuItem fileItem = (ToolStripMenuItem)Shell.MainMenuStrip.Items["File"]; RootWorkItem.UIExtensionSites.RegisterSite("FileDown", fileItem.DropDownItems); m_mdiApp = new MdiWorkspace(Shell); RootWorkItem.Workspaces.Add(m_mdiApp, "MdiWorkspace"); } } }
using System; using System.Windows.Forms; using Microsoft.Practices.CompositeUI; using Microsoft.Practices.CompositeUI.Commands; using Microsoft.Practices.CompositeUI.SmartParts; using Microsoft.Practices.ObjectBuilder; namespace MdiApp { public class TestModuleInit : ModuleInit { private WorkItem workItem; [InjectionConstructor] public TestModuleInit([ServiceDependency] WorkItem workItem) { this.workItem = workItem; } public override void Load() { ToolStripMenuItem uiMenuItem = new ToolStripMenuItem("New"); workItem.UIExtensionSites["FileDown"].Add(uiMenuItem); workItem.Commands["FileNew"].AddInvoker(uiMenuItem, "Click"); base.Load(); } [CommandHandler("FileNew")] public void OnFileNew(object sender, EventArgs e) { IWorkspace mdiWorkspace = workItem.Workspaces["MdiWorkspace"]; ButtonWorkItem bankTellerWorkItem = workItem.WorkItems.AddNew<ButtonWorkItem>(); bankTellerWorkItem.Show(mdiWorkspace); } } }
using System.Diagnostics; using Microsoft.Practices.CompositeUI; using Microsoft.Practices.CompositeUI.SmartParts; namespace MdiApp { public class ButtonWorkItem : WorkItem { public void Show(IWorkspace workspace) { ButtonControl brnCtrl = this.Items.AddNew<ButtonControl>(); workspace.Show(brnCtrl, new SmartPartInfo("Form", "Form1")); this.Activate(); } protected override void OnActivated() { Debug.WriteLine("OnActivated"); base.OnActivated(); } protected override void OnTerminated() { Debug.WriteLine("OnTerminated"); base.OnTerminated(); } protected override void OnDeactivated() { Debug.WriteLine("OnDeactivated"); base.OnDeactivated(); } } }
using System; using System.Diagnostics; using System.Windows.Forms; namespace MdiApp { public partial class ButtonControl : UserControl { public ButtonControl() { InitializeComponent(); Disposed += new EventHandler(ButtonControl_Disposed); } void ButtonControl_Disposed(object sender, EventArgs e) { Debug.WriteLine("UserControl Disposed"); } } }

~ by mdavey on September 6, 2006.

7 Responses to “CAB: MdiWorkspace”

  1. thanks for taking the time to publish a good example

  2. Coool, I am so happy I found this example :-) Thanks again.

    MT

  3. Could you please upload the sample project

  4. Hi,
    How can I custom MdiChilds?

    I had follow your example and I got MdiChilds running but I all childs are shown:
    - with the same Size;
    - in the same location;
    - without Title.

  5. It can be done using the WindowSmartPartInfo attributes when show the view in the workspace.

  6. [...] http://mdavey.wordpress.com/2006/09/06/cab-mdiworkspace/ [...]

  7. I am working mdiworkspace. still we have faced lot of issues. My functionality is before closing smart part i should ask permission to close or not? then only i close smart part. i could n’t achieve this funcationality please provide solution.

Leave a Reply