然後定義一個編輯器,其實所有這些的編輯器功能相當於一個“橋接器”,使屬性與自定義 Form 窗體關聯起來。代碼如下:
/// <summary>
/// 獲得本書更多內容,請看:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>
public class CalculatorSelectEditor : System.Drawing.Design.UITypeEditor
{
public CalculatorSelectEditor()
{
}
public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle
(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(System.ComponentModel.ItypeDescriptor
Context context,System.IServiceProvider provider,object value)
{
IWindowsFormsEditorService service = (IWindowsFormsEditorService)
provider.GetService(typeof(IWindowsFormsEditorService));
if (service == null)
{
return null;
}
FormKeyBoard form = new FormKeyBoard();
if (service.ShowDialog(form) == DialogResult.OK)
{
object strReturn = form.strReturnValue;
return strReturn;
}
return value;
}
}
此類的功能是彈出一個模式的計算器Form 窗體,與4.5.2.2 節定義的編輯器功能幾乎一樣,這 裡就不作多講,如果還有不明白的地方請回顧一下前面章節的內容。
最後定義主控件代碼類,如下所示:
/// <summary>
/// 獲得本書更多內容,請看:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>
[DefaultProperty("Money")]
[ToolboxData("<{0}:CalculatorSelectControl
runat=server></{0}:CalculatorSelectControl>")]
public class CalculatorSelectControl : WebControl
{
[Bindable(true)]
[Category("自定義計算機屬性")]
[DefaultValue("")]
[Localizable(true)]
[Editor(typeof(CalculatorSelectEditor),typeof(System.Drawing.Design. UITypeEditor))]
[Description("請輸入金額")]
public string Money
{
get
{
string s = (string)ViewState["Money"];
return ((s == null) ? "" : s);
}
set
{
ViewState["Money"] = value;
}
}
//… …
}