How to:Create a Custom WI Control

http://social.msdn.microsoft.com/Forums/en-US/tfspowertools/thread/25867a00-3d49-4ca5-998e-6e5809ea7b1a/

Op has a requirement:
"I currently have two fields. Each one of them has an integer value from 1-5. I would like to add this two numbers and get the sum of them into another field. So if field1 has the value 3 and field2 has the value 5, I would like to have in the third field the value 8 (3+5)

I am using Power Tools and I have tried many IF,WHEN,COPY statements but without any result. I would be very nice if with can be done with power tools and not writing any code to create a new custom field."

We can create a custom workitem control to fix his requirement.  To create a custom workitem control, following these steps:

1.       Create a UserControl  class library.

1)      Create a class library project (ie  MyControls)

2)      Add a UserControl to this project(ie MyControls.SumControl)

3)      Add a label "label1" in the design mode.

4)      Implement interface  IWorkItemControl, you can just use following code. In my work item type, I want Demo.sum0= Demo.sum1+ Demo.sum2

#region IWorkItemControl Members

 

        private EventHandlerList m_events;

 

        private EventHandlerList Events

        {

            get

            {

                if (m_events == null)

                {

                    m_events = new EventHandlerList();

                }

                return m_events;

            }

        }

 

        private static object EventBeforeUpdateDatasource = new object();

        public event EventHandler BeforeUpdateDatasource

        {

            add { Events.AddHandler(EventBeforeUpdateDatasource, value); }

            remove { Events.RemoveHandler(EventBeforeUpdateDatasource, value); }

        }

 

        protected virtual void OnBeforeUpdateDatasource(EventArgs eventArgs)

        {

            EventHandler handler = (EventHandler)Events[EventBeforeUpdateDatasource];

            if (handler != null)

            {

                handler.Invoke(this, eventArgs);

            }

        }

 

        private static object EventAfterUpdateDatasource = new object();

        public event EventHandler AfterUpdateDatasource

        {

            add { Events.AddHandler(EventAfterUpdateDatasource, value); }

            remove { Events.RemoveHandler(EventAfterUpdateDatasource, value); }

        }

 

        void IWorkItemControl.Clear()

        {

            label1.Text = string.Empty;

        }

 

        void IWorkItemControl.FlushToDatasource()

        {

            // nothing to do

        }

 

        void IWorkItemControl.InvalidateDatasource()

        {

 

        }

 

        private StringDictionary m_properties;

        StringDictionary IWorkItemControl.Properties

        {

            get

            {

                return m_properties;

            }

            set

            {

                m_properties = value;

            }

        }

 

        private bool m_readOnly;

        bool IWorkItemControl.ReadOnly

        {

            get

            {

                return m_readOnly;

            }

            set

            {

                m_readOnly = value;

            }

        }

 

        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;

                if (m_workItem != null)

                {

                    m_workItem.FieldChanged += new WorkItemFieldChangeEventHandler(WIChanged);

                }

            }

        }

 

        void WIChanged(object sender, WorkItemEventArgs e)

        {

            try

            {

                if (e.Field.ReferenceName == "Demo.sum1" || e.Field.ReferenceName == "Demo.sum2")

                {

                    System.Diagnostics.Debugger.Launch();

                    try

                    {

                        m_workItem.Fields["Demo.sum0"].Value = int.Parse(m_workItem.Fields["Demo.sum1"].Value.ToString())

                            + int.Parse(m_workItem.Fields["Demo.sum2"].Value.ToString());

 

                    }

                    catch { m_workItem.Fields["Demo.sum0"].Value = 0; }

 

                    label1.Text = m_workItem.Fields["Demo.sum0"].Value.ToString();

                }

            }

            catch { }

           

        }

 

        private string m_fieldName;

        string IWorkItemControl.WorkItemFieldName

        {

            get

            {

                return m_fieldName;

            }

            set

            {

                m_fieldName = value;

            }

        }

 

        #endregion

 

5)      Compile the project.

 

 

2.       Make VS2008 use this control

1)      Copy MyControls.dll to " C:\ProgramData\Microsoft\Team Foundation\Work Item Tracking\Custom Controls\9.0", If this location does not exist, create it.

2)      Create a file named "SumControl.wicc" in the same location, add following code to this file.

<?xml version="1.0"?>

<CustomControl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <Assembly> MyControls.dll </Assembly>

  <FullClassName> MyControls.SumControl </FullClassName>

</CustomControl>

 

3.       Modify the work item type.( It is better to use Power Tools to do this operation. )

1)      Add field Demo.sum0, Demo.sum1 and  Demo.sum2 in Fields Tab.

2)      Add controls in Layout Tab.  The Field Type of Demo.sum1 and  Demo.sum2 is FieldControl, and the Field Type of Demo.sum0  is SumControl

After step 2, re-open VS2008, you will see "SumControl" in the dropdown list.

3)      Save it.

4.       Create a new work item, and now you can use the Sum function.

 

In additional, it is needed to copy > MyControls.dll and SumControl.wicc to each client PC.

 

If there is anything unclear, please let me known




posted on 2009-10-01 13:41  Ruiz  阅读(4681)  评论(0编辑  收藏  举报

导航