PropertyGrid的验证 [英] PropertyGrid validation

查看:172
本文介绍了PropertyGrid的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的PropertyGrid 。当我输入一个坏的格式值(即 - 字符串转换成整数的项目),我得到一个错误信息。如果我点击确定,坏的值,直到我改变它。如果我点击取消,原来的价值又回来了。

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.

我怎么能这样做?

推荐答案

我要上加入@Crono,为什么你要,你想要什么?

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

如果你要问的我怎么能删除对话框<什么/ em>的话,我可以回答的用自己的类型转换器的:

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的的:

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

和用法

[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?

这篇关于PropertyGrid的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆