.NET Custom Control (ToolStripControlHost) Wreaks Havoc on the Designer

I need to have a MaskedTextBox in a ToolStrip, which isn't included by default, so I followed some advice I found online, and created custom control that inherits from ToolStripControlHost. What I've created works great when I'm running the application, but it really messes up the designer. By "messes up", I mean the custom control (Along with some others) disappear from the ToolStrip. Also I can no longer add new controls to the ToolStrip, and I can't select the existing controls on the ToolStrip to edit them.

Here's my class.

[DesignerCategory("code")]
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
public partial class ToolStripMaskedTextBox : ToolStripControlHost
{
    public MaskedTextBox MaskedTextBox
    {
        get { return Control as MaskedTextBox; }
    }

    public ToolStripMaskedTextBox()
        : base(CreateControlInstance()) { }

    private static Control CreateControlInstance()
    {
        MaskedTextBox mtb = new MaskedTextBox();
        mtb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        mtb.MinimumSize = new System.Drawing.Size(100, 16);
        mtb.PasswordChar = '*';
        return mtb;
    }
}

Any help on what I might be doing wrong that's giving the designer a hard time would be appreciated.

Addition Info

Now when I open my class file in Visual Studio, I get a warning page with the following error:

Constructor on type 'System.Windows.Forms.ToolStripControlHost' not found. 

Addition Info 2

The problem only occurs after building the solution. I can get the designer working correctly by modifying the Form.Designer.cs file in even the smallest way. Like adding a single space. From there on out the designer will work fine. That is until I build the solution. Then the designer freezes up again. None of the controls on the form can be edited.

According to the exception

Constructor on type 'System.Windows.Forms.ToolStripControlHost' not found. 

I found some information on the MSDN Forum.

This happends because the ToolStripControlHost class does not have a constructor with no parameter.

To solve this problem, you can create your own ToolStripControlHost with a none-parameter constructor and make the ToolStripMaskedTextBox inherited from your ToolStripControlHost. Try something like the following

//Declare a class that inherits from ToolStripControlHost.
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
public class ToolStripMaskedTextBox : MyCustomToolStripControlHost
{
    // Call the base constructor passing in a MaskedTextBox instance.
    public ToolStripMaskedTextBox() : base(CreateControlInstance()) { }

    public MaskedTextBox MaskedTextBox
    {
        get
        {
            return Control as MaskedTextBox;
        }
    }


    private static Control CreateControlInstance()
    {
        MaskedTextBox mtb = new MaskedTextBox();
        mtb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        mtb.MinimumSize = new System.Drawing.Size(100, 16);
        mtb.PasswordChar = '*';
        return mtb;
    }
}

public class MyCustomToolStripControlHost : ToolStripControlHost
{
    public MyCustomToolStripControlHost()
        : base(new Control())
    {
    }
    public MyCustomToolStripControlHost(Control c)
        : base(c)
    {
    }
}
This will fix the problem with your exception.

The Problem with the Forms Designer (ToolStripMaskedTextBox is not visible after running the app) is not solved but you can close the designer and open the file again.

Then you can go on without any problems.

Hope this helps

c#, According to the exception. Constructor on type 'System.Windows.Forms.​ToolStripControlHost' not found. I found some information on the MSDN Forum. .NET Custom Control (ToolStripControlHost) Wreaks Havoc on the Designer. Ask Question Asked 8 years, 5 months ago. Active 1 year, 4 months ago.

I've used dknaack's solution, but placed MyCustomToolStripControlHost class in a separate file in System.Windows.Forms namespace. And...

First: it works - no exception. Then: my control is visible in designer as well, so it's a jackpot.

.NET Custom Control (ToolStripControlHost) Wreaks Havoc , NET Custom Control (ToolStripControlHost) Wreaks Havoc on the Designer - c#. that contains the derived ToolStripControlHost. If you drag&drop the derived ToolStripControlHost onto a ToolStrip control on a form, you will find that it could not be added to the ToolStrip control at all. It is because the designer of ToolStrip does not support such a design-time function. In fact, a common usage is to add

In this link, the answer was that the objects that implement "blah" interface must have a parameter-less constructor. Give it a try.

ToolStrip Technology Summary, NET Custom Control (ToolStripControlHost) Wreaks Havoc on the Designer. Question. I need to have a MaskedTextBox in a ToolStrip, which isn't included by​  Note that the items of a ToolStrip (i.e., ToolStripItem-derived elements) are Components, not Controls. To add a Control-derived object to a ToolStrip (or, in our case, a ToolStripDropDown) we use the TooStripControlHost class. Using TooStripControlHost to host our custom control, we end up with the following design:

FWIW: I also succeeded with dknaack's solution above, but only after I realized I was looking for the custom ToolStrip Control in the wrong place. The custom control doesn't show up in the Toolbox itself. Rather it shows up in the dropdown list of components under the "Add ToolStripButton" that appears on the ToolStrip when it is selected (in the designer).

NET Framework · Windows Forms · Windows Forms controls This topic summarizes information about the ToolStrip control and A more consistent design-time behavior that contains task lists and item collection editors. Hosting of ToolStrip , traditional, or custom controls using ToolStripControlHost . I have a Windows Form managed by the form designer. I'd like to include a custom user control, also managed by the form designer. But I'm unable to get the custom control to show up in the Toolbox, even if I try manually adding it under the "Choose Items" dialog. Both form and control are in the same assembly.

I found a solution of designer's problem. https://dobon.net/vb/dotnet/control/tschdesigneravail.html#tips_comment (Japanese)

All you need is just make a class derived from ToolStrip and substitute it.

class DesignerFriendlyToolStrip : ToolStrip { }

var ts = new DesignerFriendlyToolStrip();
ts.Items.Add(toolStripMaskedTextBox);
form.Controls.Add(ts);

I don't know why this is effective. Anyone knows...?

Track if the cursor is over one of the areas like the button or the pushpin. If the control is in design mode, then resize to show the panel, and if in runtime, then show the ToolStripDropDown. But wait, normally, when you click the control at design time, it just selects the control.

.NET Custom Control (ToolStripControlHost) Wreaks Havoc on the Designer 6 Add sub controls to custom control after click the “OK” button of the “CollectionEditor” window in design time

While this doesn't affect the behavior of the form or control (the sequence is irrelevant), it wreaks havoc on any merge/diff tools you are using because every resequencing is treated as a change. This project is written with.NET 3.5 and has not been tested with any other configurations that support LINQ.

The IExtenderProvider is a useful interface for extending other controls on the designer surface. The code demonstrated in this article provides a solution to remove the shortcoming of the Visual Studio .NET IDE. Through the use of a custom CodeDomSerializer, a proper method of handling the limitations of the Visual Studio IDE has been found.

Comments
  • Rebuild the solution, restart VS, and see if that helps.
  • @SLaks - I've tied that, and sometimes it works, and sometimes it doesn't. The problem seems to be completely random.
  • Have a look at *.Designer.cs after you saved the form and look if it is serialized correctly.
  • @Olivier - Which designer file should I be looking at? The one for the form the control has been placed on? Is there something odd I should be looking for?
  • MyForm.Designer.cs. Check the InitializeComponent method of your form the control has been placed on.
  • This did solve the constructor problem, but unfortunately didn't solve the designer problem.
  • That looks like a bug, even on the msdn forum no one knows how to solve this. I tried so many things too
  • For me, the designer problem only occurred when the ToolStripMaskedTextBox control was located in the same project as the form I tried to use it on. When I put it into a separate project, the designer problem went away, without having to resort to using the System.Windows.Forms namespace as Harry suggested.
  • Thats what i said in my answer (parameterless-constructor)