.NET自定义控制(ToolStripControlHost)造成十分严重的设计浩劫 [英] .NET Custom Control (ToolStripControlHost) Wreaks Havoc on the Designer

查看:1106
本文介绍了.NET自定义控制(ToolStripControlHost)造成十分严重的设计浩劫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有一个ToolStrip的,这是默认情况下不包括在MaskedTextBox中,所以我也跟着一些建议,我在网上找到,并创建了自ToolStripControlHost继承自定义控件。当我运行应用程序我已经创建的伟大工程,但它确实打乱了设计师。由食堂,我指的是自定义的控制(以及一些其他人)从ToolStrip的消失。此外,我不能再增加新的控件ToolStrip的,我不能选择在ToolStrip上现有的控制对其进行编辑。

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.

下面是我的课。

[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.

附加信息

现在时我在Visual Studio中打开我的类文件,我得到以下错误警告页面:

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. 



附加信息2

该问题只构建解决方案后发生。我可以设计在即使是最小的方式修改Form.Designer.cs文件正常工作。如添加一个空格。从那里出来的设计师将正常工作。也就是说,直到我构建解决方案。那么设计师再次冻结了。无可以编辑窗体上的控件。

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.

推荐答案

据例外

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



我发现在MSDN论坛的一些信息。

I found some information on the MSDN Forum.

这happends因为 ToolStripControlHost 类没有不带参数的构造函数。

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

要解决这个问题,你可以创建一个无参数的构造函数自己ToolStripControlHost,使ToolStripMaskedTextBox从ToolStripControlHost继承。尝试类似以下

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)
    {
    }
}



这将解决这个问题,您的例外。



与形式的问题设计师(ToolStripMaskedTextBox是不运行应用程序后可见的)不就解决了,但你可以关闭设计并再次打开该文件。

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.

希望这有助于

这篇关于.NET自定义控制(ToolStripControlHost)造成十分严重的设计浩劫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆