关闭

改变 PropertyGrid 控件的编辑风格(1)——加入日期控件-.NET教程,组件控件开发

926人阅读 评论(0) 收藏 举报

适用场合:

编辑日期类型数据

 

 

步骤一:定义从uitypeeditor 派生的类,示例如下:

 

 

using system;

using system.windows.forms;

using system.drawing.design;

using system.windows.forms.design;

 

 

namespace blog.csdn.net.zhangyuk

{

     /// <summary>

     /// 在 propertygrid 上显示日期控件

     ///

     /// </summary>

     public class propertygriddateitem : uitypeeditor

     {

         monthcalendar datecontrol = new monthcalendar();

 

 

         public propertygriddateitem()

         {

              datecontrol.maxselectioncount = 1;

         }

 

 

         public override uitypeeditoreditstyle geteditstyle(

system.componentmodel.itypedescriptorcontext context)

         {

              return uitypeeditoreditstyle.dropdown;

         }

        

         public override object editvalue(

system.componentmodel.itypedescriptorcontext context,

system.iserviceprovider provider,

object value)

         {      

              try

              {

                   iwindowsformseditorservice edsvc = (iwindowsformseditorservice)

provider.getservice(typeof(iwindowsformseditorservice));

                   if( edsvc != null )

                   {

                       if( value is string )

                       {

                            datecontrol.selectionstart = datetime.parse( value as string );

                            edsvc.dropdowncontrol( datecontrol );

                            return datecontrol.selectionstart.toshortdatestring();

                       }

                       else if( value is datetime )

                       {

                            datecontrol.selectionstart = (datetime)value;

                            edsvc.dropdowncontrol( datecontrol );

                            return datecontrol.selectionstart;

                       }

                   }

              }

              catch( exception ex )

              {

                   system.console.writeline( "propertygriddateitem error : " + ex.message );

                   return value;

              }

              return value;

         }

     }

}

 

 

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

namespace blog.csdn.net.zhangyuk

{

         public class someproperties

     {

         private string _finished_time   = "";

                   ……

         [

description("完成时间"),

              category("属性"),

            editorattribute(typeof(propertygriddateitem),

typeof(system.drawing.design.uitypeeditor))

         ]

         ]

         public string 完成时间

         {

              get { return _finished_date; }

              set { _finished_date = value;}

         }

         ……

         }

}

 

 

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

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

         {

            this.propertygrid1.selectedobject = new someproperties();

         }  

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:844638次
    • 积分:12097
    • 等级:
    • 排名:第1281名
    • 原创:209篇
    • 转载:193篇
    • 译文:0篇
    • 评论:329条
    最新评论