改变 propertygrid 控件的编辑风格(4)——加入选择列表

适用场合:

限制选择输入

 

 

步骤一:定义从uitypeeditor 继承的抽象类:comboboxitemtypeconvert。示例如下:

using system;

using system.collections;

using system.componentmodel;

 

 

namespace blog.csdn.net.zhangyuk

{

     /// imstypeconvert 的摘要说明。

     /// </summary>

     public abstract class comboboxitemtypeconvert : typeconverter

     {

         public hashtable _hash = null;

 

 

         public comboboxitemtypeconvert()

         {

              _hash = new hashtable();

              getconverthash();

         }

 

 

         public abstract void getconverthash();

 

 

         public override bool getstandardvaluessupported( itypedescriptorcontext context )

         {

              return true;

         }

 

 

         public override standardvaluescollection getstandardvalues(

itypedescriptorcontext context)

         {

              int [] ids = new int [ _hash.values.count ];

 

 

              int i=0;

              foreach (dictionaryentry myde in _hash)

              {

                   ids[i++] = (int)(myde.key);

              }

 

 

              return new standardvaluescollection( ids );

         }

 

         public override bool canconvertfrom( itypedescriptorcontext context, type sourcetype)

         {

              if (sourcetype == typeof(string))

              {

                   return true;

              }

              return base.canconvertfrom(context, sourcetype);

         }

 

 

         public override object convertfrom(

itypedescriptorcontext context, 

system.globalization.cultureinfo culture,

object v )

         {

              if (v is string)

              {

                   foreach (dictionaryentry myde in _hash)

                   {

                       if( myde.value.equals((v.tostring())) )

                            return myde.key;

                   }

              }

              return base.convertfrom(context, culture, v);

         }

 

 

         public override object convertto( 

itypedescriptorcontext context, 

system.globalization.cultureinfo culture, object v ,

type destinationtype)

         { 

              if (destinationtype == typeof(string))

              {

                   foreach (dictionaryentry myde in _hash)

                   {

                       if( myde.key.equals(v) )

                            return myde.value.tostring();

                   }

                   return "";

              }

              return base.convertto(context, culture, v, destinationtype);

         }

 

 

         public override bool getstandardvaluesexclusive(

              itypedescriptorcontext context)

         {

              return false;

         }       

     }

}

 

 

步骤二:定义 comboboxitemtypeconvert 的派生类,派生类中实现父类的抽象方法:

public abstract void getconverthash();

示例如下:

 

 

using system;

using system.collections;

using system.componentmodel;

 

 

namespace blog.csdn.net.zhangyuk

{

     public class propertygridboolitem : comboboxitemtypeconvert

     {

         public override void getconverthash()

         {

              _hash.add(0,"是");

              _hash.add(1,"否");

         }

     }

 

 

     public class propertygridcomboboxitem : comboboxitemtypeconvert

     {

         public override void getconverthash()

         {

              _hash.add(0,"炒肝");

              _hash.add(1,"豆汁");

              _hash.add(2,"灌肠");

         }

     }

}

 

 

步骤三:编辑属性类,指定编辑属性。示例如下:

namespace blog.csdn.net.zhangyuk

{

         public class someproperties

     {

         private string _finished_time   = "";

                   ……

         // 布尔

         bool _bool = true;

         [

              description("布尔"),

              category("属性"),

              typeconverter(typeof( propertygridboolitem ))

         ]

         public int 布尔

         {

              get { return _bool == true ? 0 : 1; }

              set { _bool = (value == 0 ? true : false); }

         }

 

 

         // 选择列表

         int _comboboxitems = 0;

         [

              description("选择列表"),

              category("属性"),

              typeconverter(typeof( propertygridcomboboxitem ))

         ]

         public int 选择列表

         {

              get { return _comboboxitems;  }

              set { _comboboxitems = value; }

         }

         ……

         }

}

 

 

步骤四:设置propertygrid的属性对象。示例如下:

         private void form1_load(object sender, system.eventargs e)

         {

            this.propertygrid1.selectedobject = new someproperties();

         } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值