Search code examples
c#visual-studioclassdrag-and-dropcom-object

How to make a class or DLL I created appear on VS toolbox


I'm aware that once you build a UserControl, it automatically appears on the visual studio's toolbox, hence you can drag and drop it anytime. But how can one do this on a non-visual class (like a BackgroundWorker or a Timer)?

I created a class (called StationMonitor) that has properties and raises events. I'm sharing it with my colleagues and we wanted to kill time by eliminating programmatic instantiation of the object. Basically, we wanted this StationMonitor to be drag-and-droppable from toolbox like the BackgroundWoker and Timer.

I'm guessing there's something like below to do (or correct me if I'm wrong)

    [Something From Visual Studio="Something" version yada yada]
    public class StationMonitor {
       // everything here
    }

Solution

  • To include a non-visual class in the Toolbox you need to derive it from Component

    using System.ComponentModel;
    ...   
    
    class StationMonitor : Component
    {
       ...   
       ...   
    }
    

    enter image description here

    Or, if you can't derive from a class since you are already inheriting another one, implement the IComponent interface, which of course is a little more tedious..

    Interesting reads here.