我写了个自定义控件,给这个控件添加了一个自定义类的属性。现在只要把这个控件放在窗体上,那么就会在Form1.Designer.cs里面自动产生这个控件的一些初始化赋值代码。这里面会包括我这个自定义类,但由于这个类是运行时初始化的,所以在设计界面中总会报错。有没有办法让我这个自定义控件在设计器属性窗体中不显示这个类?就是这个属性我只在程序中使用,不希望他在设计画面时候总生成自动代码而爆出“序列化失败”之类的错误。

解决方案 »

  1.   

    这样,中括号里面的        [Browsable(false)]
            private bool allowdeletekey = false;
            public bool AllowDeleteKey
            {
                get { return this.allowdeletekey; }
                set { this.allowdeletekey = value; }
            }
      

  2.   


    这样虽然能够不显示了,但是还是会自动添加一些代码进去。            stepPage6.Right = 200F;
                stepPage6.Width = 100F;
                stepPage6.X = 100F;
                stepPage6.Y = 100F;
                this.drawPanel1.stepPageSelected = stepPage6;这个steppage是我的类,stepPage6是自动生成的,这几句要是不删除,就还会报“为标记为可序列化”错。每次不管我添加什么事件时候都会自动生成一遍,然后就报错。
      

  3.   


    // 给属性添加标记
    [Browsable(false)]另外代码中用this.DesignMode约束,此属性为true表示现在是设计期间而不是运行期间
      

  4.   

    public bool AllowDeleteKey
            {
                get { return this.allowdeletekey; }
                set { this.allowdeletekey = value; }
            }
    这里还要加一个默认值,只要是默认值,就不加代码,忘记属性名了,去搜下
      

  5.   

    public StepPage stepPageSelected
            {
                get
                {
                    if (mainClass == null || mainClass.MainList == null)
                    {
                        return null;
                    }                if (mainClass.MainList.Count <= mainClass.intSelectedPage)
                    {
                        return null;
                    }
                    if (mainClass.intSelectedPage < 0)
                    {
                        return null;
                    }
                    return mainClass.MainList[mainClass.intSelectedPage] as StepPage;
                }
                set
                {
                    if (mainClass != null && mainClass.MainList != null && mainClass.MainList.Count > mainClass.intSelectedPage && mainClass.intSelectedPage >= 0)
                    {
                        mainClass.MainList[mainClass.intSelectedPage] = value as StepPage;
                    }            }
            }属性里面有具体代码,不能直接加默认值,这个属性不是起封装作用的。this.DesignMode这个也不能让编辑器不自动生成代码啊,只要它生成代码就出错的。
      

  6.   


     [DesignerSerializationVisibility(DesignerSerializationVisibility.Content/*可修改*/), MergableProperty(false)]
    试试这个标记