我有一台PropertyGrid。当我输入一个格式错误的值(例如,一个字符串进入一个整数项)时,我得到一条错误信息。如果我点击"OK",坏值就会一直保留到我改变它为止。如果我点击“取消”,原始值又回来了。
我想要控制按钮,这样点击“确定”也会设置回原始值,而不是像取消按钮那样显示坏值。
我该怎么做呢?
发布于 2014-04-22 20:47:19
简短的回答是:你不能。属性网格不支持修改它。
但是,如果您觉得心情不好,这里有一些示例代码演示了如何使用此对话框。当然,使用所有这些都要自负风险。
如何使用此实用程序类:
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(); }
}
}https://stackoverflow.com/questions/23219139
复制相似问题