Rich Newman

July 27, 2007

Composite Application Block MdiWorkspace Sample

Filed under: .net, c#, CAB, Composite Application Block, dotnet, technology, User Interface — richnewman @ 9:47 pm


As has been noted on some other blogs, there is no sample provided for the MdiWorkspace in the Composite Application Block. There are a couple of examples elsewhere, but these seem a little overcomplicated, and I had difficulty getting them running. Also there doesn’t seem to be an example that shows how to use a SmartPartInfo object with the MdiWorkspace.

As a result I have put together a simplified MDI workspace example.

The Application

The screenshot below shows this running. Every time you hit File/New a new child window appears in the parent:



The code for this contains a bog-standard form (‘MdiForm’) and user control (‘ChildControl’). The form has a MenuStrip on it, with a ‘File’ menu item at the top level, and then a ‘New’ menu item in the drop down from ‘File’. There’s no user-entered code in the user control or the form.

The application is started using a standard CAB ShellApp class, with the MdiForm used as the shell form. This has functionality in the ‘AfterShellCreated’ method to hook up a CommandHandler to the ‘New’ menu item, and to instantiate an MdiWorkspace on the shell form as below:

public class ShellApp : FormShellApplication<WorkItem, MdiForm>
    public static void Main()
        new ShellApp().Run();
    protected override void AfterShellCreated()
        // Get a reference to the New' menu item already on the menu
        ToolStripMenuItem fileToolStripMenuItem = (ToolStripMenuItem)Shell.MainMenuStrip.Items["fileToolStripMenuItem"];
        ToolStripMenuItem newToolStripMenuItem = (ToolStripMenuItem)fileToolStripMenuItem.DropDownItems["newToolStripMenuItem"];

        // Hook up the FileNew command (in CommandHandlers) to the click event of
        // the New menu item
        RootWorkItem.Commands["FileNew"].AddInvoker(newToolStripMenuItem, "Click");
        // Create an MdiWorkspace in the shell
        RootWorkItem.Workspaces.Add(new MdiWorkspace(Shell), "MdiWorkspace");

Finally a CommandHandlers class contains the handler for the ‘New’ menu item as below. This adds a new ChildControl to the WorkItem for the application, and then shows it in the MdiWorkspace. It also instantiate a WindowSmartPartInfo object and sets a few random properties of the child control using it:

public class CommandHandlers
    private WorkItem workItem;
    public CommandHandlers([ServiceDependency] WorkItem workItem)
        this.workItem = workItem;
    public void OnFileNew(object sender, EventArgs e)
        IWorkspace mdiWorkspace = workItem.Workspaces["MdiWorkspace"];
        ChildControl childControl = this.workItem.Items.AddNew<ChildControl>();
        // A SmartPartInfo object lets you set various properties of the child control
        // you are displaying, relative to the workspace it's being shown in.
        // The WindowSmartPartInfo is used by the MdiWorkspace, and lets you set stuff
        // like caption, location, icon, size, and whether minimize/maximize/control boxes are present.
        WindowSmartPartInfo smartPartInfo = new WindowSmartPartInfo();
        smartPartInfo.Title = "Child Form";
        smartPartInfo.Location = new System.Drawing.Point(10, 10);
        smartPartInfo.ControlBox = false;
        mdiWorkspace.Show(childControl, smartPartInfo);

The code is available for download.


Blog at