是否有可能创建一个自定义的控件属性仅在设计时设定的? [英] Is it possible to create a custom control property only settable at design time?

查看:94
本文介绍了是否有可能创建一个自定义的控件属性仅在设计时设定的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
C#的控件可以有一个只有设计时的财产?






我想创建一个只能在设计时是有可能吗?


解决方案

是...在你的类,创建一个私有字段来标识,如果你的财产已初步设置与否。然后让你的标准属性getter和setter。然而,在二传手,使用许可证管理器检测到您使用的是... designtime操作的模式VS运行。然后检查。如果设计时,或者该字段从未设置还总是允许。一旦,然后设置标志作为被设置。这会因为控制的窗体设计器的实例化过程是必要的,它必须从了.Designer.cs代码至少一次设置,但在此之后,忽略任何试图改变它 - 通过设置标志

 私人布尔IsCreated = FALSE; 

私人字符串myVar1;
公共字符串MyVar1
{
{返回myVar1; }
集合{
如果(LicenseManager.UsageMode == LicenseUsageMode.Designtime
|| IsCreated!)
{
myVar1 =价值;
IsCreated = TRUE;
}
}

现在,你仍然可以允许的改变,但你就必须通过在你的类中创建一个自定义的方法来做到这一点,因为IsCreated标志是私人的,不受保护儿童继承渣土周围。你将不得不清除该标志,然后重置为新的字符串(或其他)珍惜你的财产是持有


Possible Duplicate:
Can a C# control have a Design Time only property?

I want to create a custom control property that can only be set at design time is it possible ?

解决方案

Yes... In your class, create a private field to identify if your property has been initially "set" or not. Then have your standard property getter and setter. However, in the setter, use the license manager to detect which mode you are operating under ... designtime vs runtime. Then check. Always allow if in Design-Time, OR if the field has never been set yet. Once in, then set the flag as being set. This will be needed since during the form designer's instantiation of the controls, it has to be set at least ONCE from the .Designer.cs code, but after that, ignore any attempts to change it -- via setting the flag.

    private Boolean IsCreated = false;

    private String myVar1;
    public String MyVar1
    {
       get { return myVar1; }
       set {
              if (LicenseManager.UsageMode ==  LicenseUsageMode.Designtime 
                    || !IsCreated)
              {
                  myVar1 = value;
                  IsCreated = true;
              }
    }

Now, you could still allow a change, but you would have to do it via a custom method created in your class since the "IsCreated" flag is PRIVATE and not PROTECTED for child-inheritance to muck around with. You would have to clear the flag, then reset to the new string (or whatever ) value your property was to hold

这篇关于是否有可能创建一个自定义的控件属性仅在设计时设定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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