How To: Write Team System Custom Control
How To: Write Team System Custom Control
In this tutorial I'll show how to create simple Team System Custom Control.
Create new project:
Add reference to:
(Version 9.0.0.0 for Visual Studio 2008)
You can find those dll's in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies
Add using
using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
Add new User Control and lable.
public partial class Header : UserControl, IWorkItemControl
{
private System.Collections.Specialized.StringDictionary properties;
private bool readOnly = false;
public Header()
{
InitializeComponent();
}
#region IWorkItemControl Members
/// Raise this events before updating WorkItem object with values. When value is changed by a control, work item form asks all controls (except current control) to refresh their display values (by calling InvalidateDatasource) in case if affects other controls
public event EventHandler BeforeUpdateDatasource;
/// Raise this event after updating WorkItem object with values. When value is changed by a control, work item form asks all controls (except current control) to refresh their display values (by calling InvalidateDatasource) in case if affects other controls
public event EventHandler AfterUpdateDatasource;
/// Control is asked to clear its contents
void IWorkItemControl.Clear()
{
}
/// Control is requested to flush any data to workitem object. This usually happens during save operation or when the form is left. In most cases data will be written to workitem immediately on change and hence this will not need implementation. Some customers want a way to do operations during save, and this is the closest thing we got. If you do need a way to react to before-save & after-save events, pls let us know in forums given below and we'll consider for future revision.
void IWorkItemControl.FlushToDatasource()
{
this.BeforeUpdateDatasource(this, EventArgs.Empty);
this.AfterUpdateDatasource(this, EventArgs.Empty);
}
/// Asks control to invalidate the contents and redraw. At this point, control can read from work item object and display/refresh data.
void IWorkItemControl.InvalidateDatasource()
{
try
{
string WitType = m_workItem.Type.Name;
label1.Text = WitType;
switch (WitType)
{
case "Bug":
this.BackColor = Color.Blue;
break;
case "Task":
this.BackColor = Color.Brown;
break;
case "Requirement":
this.BackColor = Color.Pink;
break;
default:
this.BackColor = Color.White;
break;
}
}
catch (Exception ex)
{
}
}
/// A property bag of all attributes specified in work item form xml for this control. Custom attributes are allowed and can be used to pass parameters specific for this control from work item type xml.
System.Collections.Specialized.StringDictionary IWorkItemControl.Properties
{
get { return properties; }
set { properties = value; }
}
/// Tells the control to display in readonly mode.
bool IWorkItemControl.ReadOnly
{
get { return readOnly; }
set { readOnly = value; OnReadOnlyChanged(); }
}
/// Gives pointer to IServiceProvider if you intended to access Document service or VS Services. If services are not needed, do nothing in this method.
private IServiceProvider m_serviceProvider;
void IWorkItemControl.SetSite(IServiceProvider serviceProvider)
{
m_serviceProvider = serviceProvider;
}
private WorkItem m_workItem;
object IWorkItemControl.WorkItemDatasource
{
get
{
return m_workItem;
}
set
{
m_workItem = (WorkItem)value;
}
}
/// The field name if the control is associated with a field name in work item form xml. A custom control can be associated with 0 or 1 work item field.
private string m_fieldName;
string IWorkItemControl.WorkItemFieldName
{
get
{
return m_fieldName;
}
set
{
m_fieldName = value;
}
}
#endregion
private bool IsInitialized()
{
return (!this.IsDisposed &&
m_workItem != null &&
!string.IsNullOrEmpty(m_fieldName) &&
m_workItem.Fields.Contains(m_fieldName));
}
private void OnReadOnlyChanged()
{
}
}
You can download the Project from Here