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);
        }
    }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值