改变 propertygrid 控件的编辑风格(1)——加入日期控件
张昱[email protected]
效果:
适用场合:
编辑日期类型数据
步骤一:定义从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();
}
新闻热点
疑难解答