Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi. First, I'm sorry for my weak language

in this code i create a componet in design time ,property ComplexObject(class) correctly initialize on the my form with all detailes in the InitializeComponent() method Form.

But when I using the grid property on the form,it not initialize in the initialization component () method Form.

What is the reason?

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace WindowsFormsApplication18
{
    [DefaultEvent("NumofClicksChanged")]
    [DefaultProperty("NumOfClicks")]
    [ToolboxBitmap(typeof(ButtonEx), "ButtonEx.png")]


  
    public class ButtonEx : Button
    {

        private System.ComponentModel.Container components = null; 
        public ButtonEx()
        {
            components = new System.ComponentModel.Container();
            _numOfClicks = 10;
            _ComplexObject = new Info();
            _grid = new DataGridView();
        }
        private int _numOfClicks;


        public event EventHandler NumofClicksChanged;


        [DefaultValue(10)]
        [Category("Special Properties")]
        [Description("Tells how many time the button has been clicked")]
        [DisplayName("Number of Clicks")]
        public int NumOfClicks
        {
            get { return _numOfClicks; }
            set
            {
                if (_numOfClicks != value)
                {
                    OnNumofClicksChanged(EventArgs.Empty);
                    _numOfClicks = value;
                }
            }
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            NumOfClicks++;
        }

        protected virtual void OnNumofClicksChanged(EventArgs eventArgs)
        {
            if (NumofClicksChanged != null)
            {
                NumofClicksChanged(this, eventArgs);
            }
        }

        string s;
        [Browsable(false)]
        public string ComplexProperty
        {
            get { return s; }
            set { s = value; }
        }

        private Color c;
        [DefaultValue(typeof(Color), "#FFBA00")]

        public System.Drawing.Color ColorProperty
        {
            get { return c; }
            set { c = value; }
        }

        System.Drawing.Font f;
        public System.Drawing.Font FontProperty
        {
            get
            {
                if (f == null)
                    return this.Font;
                else return f;
            }
            set { f = value; }
        }

        void ResetFontProperty()
        {
            f = null;
        }
        bool ShouldSerializeFontProperty()
        {
            return f == null ? false : true;
        }


        private string spVar;

        [ParenthesizePropertyName(true)]
        public string SpecialProperty
        {
            get { return spVar; }
            set { spVar = value; }
        }

        private int refreshOthers;
        [RefreshProperties(RefreshProperties.All)]
        public int RefreshOtherPropeties
        {
            get { return refreshOthers; }
            set { refreshOthers = value; }
        }


        private string v;

        [ReadOnly(true)]
        public string ReadOnlyProperty
        {
            get { return v; }
            set { v = value; }
        }

        private string v1;

        [RefreshProperties(RefreshProperties.All)]
        public string RefreshOtherProperty
        {
            get { return v1; }
            set { v1 = value; }
        }

        private string v2;

        [NotifyParentProperty(true)]
        public string NotifyParentProperty
        {
            get { return v2; }
            set { v2 = value; }
        }

        private string v3;

        [DesignOnly(true)]
        public string DesignTimeOnlyProperty
        {
            get { return v3; }
            set { v3 = value; }
        }

        private Info _ComplexObject;
        [TypeConverter(typeof(ComplexObjectConverter))]                            
        public Info ComplexObject
        {
            get { return _ComplexObject; }
            set { _ComplexObject = value; }
        }


        private System.Windows.Forms.DataGridView _grid;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [TypeConverter(typeof(DataGridViewConverter))]                                    
        public System.Windows.Forms.DataGridView grid
        {
            get { return _grid; }
            set { _grid = value; }
        }

    }


    public class Info
    {
        int i;
        string s;
        public int IntProperty
        {
            get { return i; }
            set { i = value; }
        }
        public string StringProperty
        {
            get { return s; }
            set { s = value; }
        }
    }


    public class ComplexObjectConverter:System.ComponentModel.TypeConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(typeof(Info));
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

    }


    public class DataGridViewConverter : System.ComponentModel.TypeConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(typeof(DataGridView));
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

    }


}
Posted
Updated 23-Mar-11 3:54am
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900