locked
How to add .NET component type of toolbox item in Addin in VS 2010? RRS feed

  • Question

  • Hi all,

    We have two ways to add toolbox item:one is by EnvDTE, the other is by ServiceProvider.


    when we do it by EnvDTE, we can mark the toolbox type for example .NET component like:

    tbt.ToolBoxItems.Add("UserControl1", "D:\\UseControl1.dll", EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);

    after this, when we drag this toolbox item , UserControl1 can be dragged into form designer without any my own code needed.


    however, if i want to do this in ServiceProvider of Addin, how could i do?

    for i haven't found the interface/property where can input something like EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent. (piece of code is written below) 

                        System.Drawing.Design.IToolboxService tbs = serviceProvider.GetService(typeof(System.Drawing.Design.IToolboxService)) as System.Drawing.Design.IToolboxService;

     

                        System.Drawing.Design.ToolboxItem tbItem = new System.Drawing.Design.ToolboxItem();

                        System.Reflection.AssemblyName tbItemAssemblyName = new System.Reflection.AssemblyName("UserControl1");

                        tbItemAssemblyName.CodeBase = "D:\\UseControl1.dll";

                        tbItem.AssemblyName = tbItemAssemblyName;

                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap("D:\\mybitmap.bmp");

                        tbItem.Bitmap = bitmap;

                        tbItem.DisplayName = "MyToolboxItem";

                        tbs.AddToolboxItem(tbItem, "Our Own Control");

     

    by this piece of code, when i drag this toolbox item, the exception will be raised like:

    "Faided to create component 'MyToolboxItem'. the error message folllows:

    'System. UriFormatException:Invalid URI: A Dos path must be rooted, for example, 'c:\

    at system.uri.createthis(string uri, boolean dontescapte, urkind urikind)

    .....

    at system.drawing.design.tooboxitem.createcomponentcore(Idesignerhost host)

    ''

    what does this mean?

     

     

    thanks,

    Jack.


    Thanks, Jack
    Tuesday, August 17, 2010 8:29 AM

Answers

  • got solved by overwrite ToolboxItem::CreateComponentsCore().
    Thanks, Jack
    • Marked as answer by WVisualPerson Friday, August 20, 2010 3:03 AM
    Friday, August 20, 2010 3:03 AM

All replies

  • Hi Jack,

     

    Thanks for your post again.

    As far as I know, the implement drag and drop support needs ToolboxItemCreatorCallback delegate.

    And to customize toolbox by IToolboxService, please reference to this sample:

    http://msdn.microsoft.com/en-us/library/bb166486.aspx

    Hope this can help.

     

    Best Regards,

    Ziwei Chen

     

    Wednesday, August 18, 2010 8:07 AM
  • Hi Victor,

     

    This link gives the guideline toward VSPackage way. what i am using is Addin, so do you have the way to implement that?

     

    Thanks,

    Warren.


    Thanks, Jack
    Thursday, August 19, 2010 6:21 AM
  • Hi Jack,

     

    I think you can use it in add-in as well.

    Have you tried this code in OnConnection?

    protected ICollection ToolboxItemList = null;

    ToolboxItemList = ToolboxService.GetToolboxItems(Assembly.GetExecutingAssembly(), "");

    if (ToolboxItemList == null){

        throw new ApplicationException("Unable to generate a toolbox Items listing for "

    + GetType().FullName);

    }

    IToolboxService toolboxService = GetService(typeof(IToolboxService)) as IToolboxService;

    foreach (ToolboxItem itemFromList in ToolboxItemList){

        toolboxService.AddToolboxItem(itemFromList, CategoryTab);

    }

    Looking forward to your reply.

    Best Regards,

    Ziwei Chen

    Thursday, August 19, 2010 6:55 AM
  • Hi Victor,

     

    Adding toolbox item is not a problem now. the problem is:

    1, why dragging a UserControl toolbox item assembly into designer doesn't work.  

    2, what's the general drag-drop way of toolbox item? there are detailed and elaborate materials for Addin?

     

    Thanks,

    Jack.


    Thanks, Jack
    Thursday, August 19, 2010 8:12 AM
  • got solved by overwrite ToolboxItem::CreateComponentsCore().
    Thanks, Jack
    • Marked as answer by WVisualPerson Friday, August 20, 2010 3:03 AM
    Friday, August 20, 2010 3:03 AM