c# propertyGrid下拉选项
2015-12-29 15:36
741 查看
实现下面效果的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);
}
}
具体代码如下
//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);
}
}
相关文章推荐
- C#中静态类与非静态类的区别
- 转载文章列表
- C#隐式实现接口成员与显示实现接口成员
- c#中文件操作Helper
- C#操作Cookie
- c#中的partial
- 1、c#对XML文件的解析
- 【C#】 之 out和ref
- C# RDLC 绑定
- C# Regex 正则表达式 匹配特定的内容
- C# RGB颜色转换为16进制
- C# 用 * 输出两个等腰三角形组成的菱形
- C# 使用 DirectoryInfo 递归指定目录中的所有目录及文件
- C# 打开指定的文件夹 记住路径中 / 与 \ 的用法
- 带有按钮并且可以执行单击事件的WINFORM窗体,体悟C#的创建过程
- C# 入门之 Hello World
- C#动态编译
- 【机房收费系统C#版】——导出Excel
- c#读取xml配置文件
- C#序列化和反序列化代码总结