自定义了一个控件
在控件里有一属性如下
        ComboBoxItemCollection items;
        
        public ComboBoxItemCollection Items
        {
            get { return items; }
            set {
                if (value == null)
                    value = new ComboBoxItemCollection();
                items = value; RecalSize(); }
        }
在窗体设置器中使用这控件把它拖进窗体就得
设计器自动生成了代码
      private void InitializeComponent()
        {

  this.comboBoxSelectItemControl1.ActiveColor = System.Drawing.Color.White;
            this.comboBoxSelectItemControl1.Items = null;
这里我想让null设计器自动生成的代码为new ComboBoxItemCollection()
需要怎么做

解决方案 »

  1.   

    你可以在这个自定义控件类的构造函数中,new ComboBoxItemCollection()
      

  2.   

    不有用的呀,应该可以在属性加一个什么标记设置的如
       [DefaultValue(..)]之类的
      

  3.   

    C# WinForm控件开发如何设置属性的默认值
      

  4.   

    在Form_Load事件中来初始化就可以设置默认值
    items=new ComboBoxItemCollection();
      

  5.   


        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();            this.items = new ComboBoxItemCollection();
            }
            
            //设计时,在属性编辑器添加项,自动生成代码
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            public ComboBoxItemCollection Items
            {
                get { return items; }
                set
                {
                    if (value == null)
                        value = new ComboBoxItemCollection();
                    items = value; RecalSize();
                }
            }        public void RecalSize()
            {
            }
            
            ComboBoxItemCollection items;
        }    public class ComboBoxItemCollection : ComboBox.ObjectCollection//不知道你的ComboBoxItemCollection 怎么定义,我随便找个代替,不影响本问题
        {
            public ComboBoxItemCollection()
                :base(new ComboBox())
            {
            }
        }
    自动生成Item代码            // 
                // userControl11
                // 
                new WindowsFormsApplication1.ComboBoxItemCollection().AddRange(new object[] {
                ((object)(resources.GetObject("userControl11.Items")))});
                this.userControl11.Location = new System.Drawing.Point(70, 53);
                this.userControl11.Name = "userControl11";
                this.userControl11.Size = new System.Drawing.Size(150, 150);
                this.userControl11.TabIndex = 0;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.userControl11);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
      

  6.   

    楼上的你的自动生成代码那,我可没有看到有生成Item的代码呀
      

  7.   

    get 

        if(items=null)
            items = new ComboBoxItemCollection();
        return items; 
    }
    set
    {
        if (value == null)
            value = new ComboBoxItemCollection();
        items = value; RecalSize();
    }