【问题标题】:PropertyGrid validationPropertyGrid 验证
【发布时间】:2014-04-22 12:09:43
【问题描述】:

我有一个PropertyGrid。当我输入格式错误的值(即 - 将字符串转换为整数项)时,我收到一条错误消息。如果我单击“确定”,则错误值会一直存在,直到我更改它为止。如果我点击“取消”,就会恢复原来的值。

我想控制按钮,因此单击“确定”也会将原始值设置回来,而不是像取消按钮那样显示错误的值。

我该怎么做?

【问题讨论】:

  • 我不禁要问:为什么?
  • 您必须编写自己的 TypeConverter 并在其 ConvertFromString() 方法中发现问题。这就是使用 PropertyGrid 的价值开始迅速降低的地方,当您发现自己想要更改其默认行为时,就该创建自己的数据输入表单了。

标签: c# winforms validation propertygrid


【解决方案1】:

我将加入 @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
{
    ...
}

但是您想要那个错误对话框(当设置/获取属性时出现异常时显示)并且您希望 Ok 按钮的工作方式与 Cancel 相同。为什么?

【讨论】:

    【解决方案2】:

    简短的回答是:你不能。属性网格不支持修改此项。

    但是,如果您心情不好,这里有一些示例代码演示了如何使用此对话框。当然,使用这一切需要您自担风险。

    如何使用这个实用程序类:

    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(); }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 1970-01-01
      • 2011-03-30
      • 2013-06-20
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多