向PropertyGrid抛出异常

时间:2013-05-23 09:51:59

标签: c# winforms exception propertygrid

我有Winforms PropertyGrid

尝试在变量中设置值时。 我想向PropertyGrid抛出错误,因为在输入无效值后出现错误。

enter image description here

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:2)

您只是throw中的set例外:

private int someProperty;
public int SomeProperty {
    get { return someProperty; }
    set {
        if((value % 3) != 0) throw new ArgumentOutOfRangeException(
            "the value must be divisible by 3");
        someProperty = value;
    }
}

产地:

enter image description here

enter image description here