PropertyGrid validation
本文关键字:validation PropertyGrid | 更新日期: 2023-09-27 17:59:31
我有一个PropertyGrid
。当我输入一个错误的格式值(即,将字符串转换为整数项)时,我会收到一条错误消息。如果单击"确定",错误值将一直保留,直到我更改为止。如果单击"取消",原始值将返回。
我想控制按钮,所以点击"确定"也会将原始值设置回原来的值,而不是像取消按钮那样显示坏值。
我该怎么做?
我会加入@Crono,你为什么想要你想要的?
如果你问我如何删除该对话框,那么我可以回答使用自己的TypeConverter:
public class IntConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if(value is string)
{
// try parse to int, do not throw exception
}
return 0; // always return something
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return value.ToString();
return base.ConvertTo(context, culture, value, destinationType); // i left it here but it should never call it
}
}
如果你问我希望我自己的对话框编辑一些东西,那么我会回答使用自己的UITypeEditor:
public class MyEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
Form1 form1 = new Form1();
form1.ShowDialog();
return form1.SomeProperty;
}
}
使用
[TypeConverter(typeof(IntConverter))]
[EditorAttribute(typeof(MyEditor), typeof(UITypeEditor))]
public int SomeProperty
{
...
}
但您希望出现错误对话框(当设置/获取属性时出现异常时会显示该对话框),并且您希望确定按钮的工作方式与Cancel
简单的答案是:你不能。属性网格不支持对此进行修改。
但是,如果您觉得心情不好,这里有一些示例代码演示如何使用此对话框。当然,使用所有这些都要自担风险。
如何使用这个实用程序类:
propertyGrid1.Site = new MySite(propertyGrid1);
propertyGrid1.SelectedObject = MyObj;
公用事业类别代码:
public class MySite : ISite, IUIService
{
public MySite(PropertyGrid propertyGrid)
{
PropertyGrid = propertyGrid;
}
public object GetService(Type serviceType)
{
if (serviceType == typeof(IUIService))
return this;
return null;
}
// this is part of IUIService
public DialogResult ShowDialog(Form form)
{
// Check the form passed here is the error dialog box.
// It's type name should be GridErrorDlg.
// You can also scan all controls and for example
// remove or modify some buttons...
DialogResult result = form.ShowDialog(PropertyGrid);
if (form.GetType().Name == "GridErrorDlg" && result == DialogResult.OK)
{
PropertyGrid.Refresh();
}
return result;
}
public PropertyGrid PropertyGrid { get; private set; }
public bool DesignMode { get { return false; } }
public IContainer Container { get { return null; } }
public bool CanShowComponentEditor(object component) { return false; }
// I've left the rest as not implemented, but make sure the whole thing works in your context...
public IComponent Component
{
get { throw new NotImplementedException(); }
}
public string Name
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public IWin32Window GetDialogOwnerWindow()
{
throw new NotImplementedException();
}
public void SetUIDirty()
{
throw new NotImplementedException();
}
public bool ShowComponentEditor(object component, IWin32Window parent)
{
throw new NotImplementedException();
}
public void ShowError(Exception ex, string message)
{
throw new NotImplementedException();
}
public void ShowError(Exception ex)
{
throw new NotImplementedException();
}
public void ShowError(string message)
{
throw new NotImplementedException();
}
public DialogResult ShowMessage(string message, string caption, MessageBoxButtons buttons)
{
throw new NotImplementedException();
}
public void ShowMessage(string message, string caption)
{
throw new NotImplementedException();
}
public void ShowMessage(string message)
{
throw new NotImplementedException();
}
public bool ShowToolWindow(Guid toolWindow)
{
throw new NotImplementedException();
}
public System.Collections.IDictionary Styles
{
get { throw new NotImplementedException(); }
}
}