How to add a custom control to the toolbox?
Asked Answered
C

1

5

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: https://i.sstatic.net/QXrrB.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

Crookes answered 27/6, 2012 at 19:47 Comment(1)
Test to add an empty constructor?Ell
B
8

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 /* }
}
Bowra answered 27/6, 2012 at 19:52 Comment(1)
Of course, forgot about the fact the designer doesn't pass parameters. =)Crookes

© 2022 - 2024 — McMap. All rights reserved.