改變 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();
}
新聞熱點
疑難解答