c# propertyGrid下拉選項


實現下面效果的propertygrid屬性下拉選擇

具體代碼如下

//form窗口類

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            properties ps = new properties();
            propertyGrid1.SelectedObject = ps;
        }
    }

 

//屬性類

    public class properties
    {
        [Category("性別"),Description("student gender"),
        TypeConverter(typeof(genderItem)) //使用自定義的屬性下拉item類
        ]
        public string Gender
        {
            get;
            set;
        }
    }

//自定義屬性下拉效果的類,該類主要繼承StringConverter類,並重載該類的一些虛擬方法

    public class genderItem:StringConverter
    {
        //true enable,false disable
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new string[] { "男", "女" }); //編輯下拉框中的items
        }

        //true: disable text editting.    false: enable text editting;
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
    }

//形成checkbox選項樣式

屬性類的代碼如下

        [Category("信息"), Description("student information"),
        Editor(typeof(CheckboxPro),typeof(System.Drawing.Design.UITypeEditor))]
        public bool IsGoodStudent
        {
            get;
            set;
        }

其中checkboxpro類定義如下,用該方法會出了checkbox窗口

    public class CheckboxPro:System.Drawing.Design.UITypeEditor
    {
        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override void PaintValue(System.Drawing.Design.PaintValueEventArgs e)
        {
            ControlPaint.DrawCheckBox(e.Graphics, e.Bounds, ButtonState.Inactive);
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM