PropertyGrid validation

妖精的绣舞 提交于 2019-12-10 11:03:10

问题


I have a PropertyGrid. When I enter a bad-format value (i.e. - a string into an integer item), I get an error message. If I click "OK", the bad value stays until I change it. If I click "Cancel", the original value is back.

I want to control the buttons so clicking "OK" will also set the original value back instead of showing the bad value like the cancel button.

How can I do that?


回答1:


I'll join @Crono on that, why do you want that what you want?

If you would ask how can I remove that dialog, then I could answer use own 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
    }
}

If you would ask I want my own dialog to edit something, then I'd answer use own 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;
    }
}

And usage is

[TypeConverter(typeof(IntConverter))]
[EditorAttribute(typeof(MyEditor), typeof(UITypeEditor))]
public int SomeProperty
{
    ...
}

But you want that error dialog (which is shown when there is exception when setting/getting property) and you want Ok button works same way as Cancel. Why?




回答2:


The short answer is: you can't. The property grid does not support modifying this.

However, if you feel in a hacky mood, here is some sample code that demonstrates how to play with this dialog box. Of course, use all this at your own risk.

How to use this utility class:

propertyGrid1.Site = new MySite(propertyGrid1);
propertyGrid1.SelectedObject = MyObj;

The utility class code:

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/propertygrid-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!