1
我試圖創建自己的UITypeEditor的,但的EditValue方法不會被調用如何創建限制字符串n個字符
public class BoundedTextEditor : UITypeEditor
{
public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.None;
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
if (value.GetType() != typeof(string)) return value;
var editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (editorService != null)
{
var textBox = new TextBox { Text = value.ToString(), Size = new Size(200, 100), MaxLength = 3 };
editorService.DropDownControl(textBox);
return textBox.Text;
}
return value;
}
}
使用這樣的PropertyGrid中編輯:
[Editor(typeof(BoundedTextEditor), typeof(UITypeEditor))]
public string KeyTip
{
get
{
return _keyTip;
}
set
{
_keyTip = value;
}
}
這裏我試圖將字符串限制爲3個字符,如果可以通過屬性定義的話會更好。