SCSF – giving a smartpart a title using attributes

When creating a [SmartPart] in SCSF, and adding it in a Tabbed Workspace, its title is empty by default. This results in a tab without a label in the workspace, not a pretty view.
The standard way to solve this is to implement the ISmartPartInfoProvider interface and then add code like this:
 

#region

ISmartPartInfoProvider Members

public ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)

{

    return new SmartPartInfo("Dashboard", "Your dashboard");

}

#endregion
 

I agree that this isn’t very much trouble, but here is the smarter solution:

I added a new class into the Infrastructure project:

public

class SCSFUserControl : UserControl, ISmartPartInfoProvider

{

  #region

ISmartPartInfoProvider Members

  public virtual ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)

  {

    Type me = GetType();

    SmartPartInfoProviderAttribute[] atts = (SmartPartInfoProviderAttribute[])me.GetCustomAttributes(typeof(SmartPartInfoProviderAttribute), true);

    if (atts.Length == 0)

    {

      return new SmartPartInfo();

    }

    return new SmartPartInfo(atts[0].Title, atts[0].Description);

  }

  #endregion

}
 

Now we have to define the SmartPartInfoProviderAttribute:

[

AttributeUsage(AttributeTargets.Class)]

public class SmartPartInfoProviderAttribute : Attribute

{

  public string Title;

  public string Description;

  public SmartPartInfoProviderAttribute() : this(string.Empty, string.Empty)

  { }

  public SmartPartInfoProviderAttribute(string title, string description)

  {
    Title = title;
    Description = description;
  }
}
 

Creating a new smartpart now becomes easy:

[

SmartPartInfoProvider("Tasks", "Task list")]

public partial class TasklistView : SCSFUserControl, ITasklistView

// Your code here

 

And it will have a label in the Tabbed Workspace.

Advertisement

About Gaston

MCT, MCSD, MCDBA, MCSE, MS Specialist
This entry was posted in Development. Bookmark the permalink.

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 )

Facebook photo

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

Connecting to %s