Saturday, May 19, 2007

Creating and Showing SmartParts

There are two scenarios for using SmartParts in a CAB application:

  • The SmartPart required for a given location on a form is known at design time.
  • The SmartPart is created and displayed dynamically at runtime, typically in response to user interaction.
If you know the location at design time, you can drag and drop the SmartPart into a suitable Workspace to achieve the required layout of the form contents. For example, you can use a TabWorkspace or a ZoneWorkspace.

To create a SmartPart

  1. Add a user control to your project.
  2. Add a reference to the Microsoft.Practices.CompositeUI.SmartParts namespace.
  3. Design the control to provide the appropriate appearance and functionality.
  4. Add the SmartPart attribute to the control class as shown in the following code.

[SmartPart]
public partial class MySmartPart : UserControl

To display a SmartPart dynamically at runtime, you can either allow a Workspace control to manage the positioning and dimensions of the SmartPart or you can explicitly designate an area in a form where the SmartPart will appear at runtime by using a SmartPartPlaceHolder control.



To display a SmartPart in a SmartPartPlaceholder

  1. Create an instance of the SmartPart user control using the AddNew method of the SmartParts or the Items collection of the WorkItem. You pass a string to the AddNew method to assign a name to the SmartPart. At runtime, the SmartPart is automatically loaded into the SmartPartPlaceHolder that specifies the SmartPart name for its SmartPartName property.
    CustomerSmartPart csp;
    csp = myWorkItem.SmartParts.AddNew("CustomerSP");


To display a SmartPart in a Workspace

  1. Create an instance of the SmartPart user control with the AddNew method of the SmartParts collection of the WorkItem.
  2. Call the Show method of the Workspace in which you want to show the SmartPart.
    public class MyWorkItem : WorkItem
    {
    protected override void OnRunStarted()
    {
    base.OnRunStarted();
    CustomerSmartPart csp = this.SmartParts.AddNew();
    Workspaces["tabbedArea"].Show(csp);
    }
    }
If you want to specify advanced preferences for how the control will appear, you can create a SmartPartInfo instance of the type that is appropriate for the workspace you are using.

No comments: