How do I manipulate ToolBox Items?

Ah! I see two light bulbs lit in my big head. You can either use

low cost DTE lenses or use a fairly involved dose of SVsToolbox

service. So which one you should use? Read on and find it out…

Use DTE

       If all you intend is basic manipulation of the ToolBox tabs and items underneath then DTE works just fine. ToolBoxTab and ToolBoxItem objects give you control over manipulating the state of the item. However the object model manipulating ToolBoxItem using DTE is minimal. The richness of DTE pertaining to Tool box item manipulation lies in its ability to add new tool box items. See code below that shows how you can get to a specific tool box item. And you can see that you do not have deeper functionality. The link below shows code for a macro code but you can easily adapt it to be used from an add-in or a VSIP package. This approach is available to both add-in and VSIP package components.

Controlling the ToolBox

Use SVsToolbox service

      This approach is available only within a context of a VSIP package. It does provide you richer access to tool box functionality. So here what you can do:

· Query for SVsToolbox service from the global service provider

· Get the IVsToolbox2 interface

· Enumerate items under a specific Tool box tab

· Get the IDataObject for each item under it.

The drawback to using SVsToolbox service is that IVsToolbox2 interface does not provide a way to access the tool box item name just like what DTE provides. The VSIP team is working on getting this fixed for future releases of Visual Studio. The code snippets below just enumerate over the items existing under the “Windows Forms” tab of the Tool Box.

The code assumes that the Tool Box is open in the IDE.

C++ code:

HRESULT hr = _Module.QueryService(SID_SVsToolbox, IID_IVsToolbox, (void**) &srpIVsToolbox);

if(SUCCEEDED(hr))

{

  hr = srpIVsToolbox->EnumItems(L"Windows Forms",&srpETI);

  if(SUCCEEDED(hr))

  {

       CComPtr<IDataObject> pData;

       int count = 0;

       while(srpETI->Next(1, &pData, NULL)==S_OK)

       {

       //Do whatever with the data object

              if(pData)

              {

                     pData.Release();

                     pData = NULL;

                     count++;

              }

       }

   }

 

  srpETI.Release();

  srpIVsToolbox.Release();

}

C# Code:

IVsToolbox2 ivToolBox =

      (IVsToolbox2)(GetService(typeof(SVsToolbox)));

//Enumerate tabs

IEnumToolboxItems pEnumItems;

ivToolBox.EnumItems("Windows Forms", out pEnumItems);

                          

Microsoft.VisualStudio.OLE.Interop.IDataObject [] pDo =

    new Microsoft.VisualStudio.OLE.Interop.IDataObject[1];

uint fetched;

int count =0;

while(pEnumItems.Next(1, pDo, out fetched)==NativeMethods.S_OK)

{

       if(pDo[0]!=null)

       {

              pDo[0] = null;

              count++;

       }

}

 

Thanks

Dr. eX

   

Comments