2

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
    }
morethanyell
  • 316
  • 3
  • 10

1 Answers1

1

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.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Thanks Taw! I will have to mark this as the answer. Life saver. However, I'd like to say that this prevented my class from being Serializable. I can no longer save an object instance of StationMonitor to my HDD. :'( – morethanyell Aug 02 '17 at 12:40
  • Hm, interesting. I'm sure it can be resolved, but can't say how. This may warrant a separate question, imo. First do have a look at [this interesting answer, though!](https://stackoverflow.com/questions/18588083/xmlserializer-ignoring-inherited-unserializable-member) – TaW Aug 02 '17 at 13:00