Composite Application Block MdiWorkspace Sample

Introduction

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:

MdiWorkspace

Code

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>
{
    [STAThread]
    public static void Main()
    {
        new ShellApp().Run();
    }
 
    protected override void AfterShellCreated()
    {
        base.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.Items.AddNew<CommandHandlers>();
        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;
    [InjectionConstructor]
    public CommandHandlers([ServiceDependency] WorkItem workItem)
    {
        this.workItem = workItem;
    }
 
    [CommandHandler("FileNew")]
    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.

References

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

Advertisements

4 thoughts on “Composite Application Block MdiWorkspace Sample

  1. Hello…i currently creating an MDI application using SCSF. the problem i faced is i can’t control the control box buttons in child view control. for example i want to control the events of maximize, minimize, restore and resize of the child view control. So, do u have any to do it?
    I currently try child control events like parentsize change or size change or other methods. but no effects.

    Help me.

  2. hi ,
    i need one help in c# windows application ,i want to know how to create a drawing workspace . i need this for simulator application. if anybody know sensd me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s