Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom control to the toolbox?

I've created a custom control, based on the Picturebox:

public class Timebar : System.Windows.Forms.PictureBox

This works correctly if I create the control manually/set all values etc. etc, at the Form's initialization method.

Now I also found this, at the top of the Toolbox: http://i.imgur.com/4KUc0.png

When I try to insert it via msvc, I get an error however.

Failed to create component 'Timebar'.  The error message follows: 
'System.MissingMethodException: Constructor on type 'SC.Timebar' not found.

This isn't exactly a huge problem with my component Timebar (as I will add that component manually), but it is with the custom Button class I want to make (something more fancy then the default).

There IS a constructor in the class:

public Timebar(Data refr)
{
    this._refr = refr;
}

How can I fix the above error?

Thanks,

~ Tgys

like image 911
Tgys Avatar asked Jun 27 '12 19:06

Tgys


1 Answers

Controls used in the designer must have a parameterless constructor. The designer needs to create one of your controls to display and allow you to manipulate it, but it has no clue as to how it should call a constructor that requires parameters.

So, what I would do is create a parameterless constructor which chains the other constructor using a default value, i.e.,

class Foo
{
    public Foo() : this(SomeType.Value) { }
    public Foo(SomeType whatever) : { /* do stuff /* }
}
like image 131
Ed S. Avatar answered Sep 28 '22 10:09

Ed S.