首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

有关PropertyGrid设置属性验证的有关问题

2012-06-22 
有关PropertyGrid设置属性验证的问题最近想用PropertyGrid控件修改对象的一些属性.例如下边的情况,Propert

有关PropertyGrid设置属性验证的问题
最近想用PropertyGrid控件修改对象的一些属性.
例如下边的情况,PropertyGrid在编辑Height属性的时候.希望能限制Height的取值范围和格式正确性.
比如限制0<Height<100,超出范围弹出提示.
这种想法完是从vs工具箱里的NumricUpDown控件中证实的.NumricUpDown.Value取值是在MaxValue和MinValue之间.超出范围会有弹窗式的错误提示.
按照现在的代码,在实现TypeConvert,重载ConvertFrom只能在信息错误的时候,对信息作出修正,而CanConvertFrom方法又只给出了需要转换的类型,没有给出具体值.
希望高手指点下具体操作.

C# code
//PropertyGrid pg;//pg.SelectedObject = new Car();public class HeightConvert : TypeConvert{  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)  {     //...  }  public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)  {     //...  }}public class Car{    [TypeConvert(typeof(HeightConvert))]    public int Height{get;set;}}


[解决办法]
C# code
    public class Tx    {        int val = 0;        public int Value        {            get            {                return val;            }            set            {                if (value >= 0 && value <= 100)                {                    val = value;                }                else                {                    throw new Exception("数值有效范围为[0,100]");                }            }        }    } 

热点排行