I have a Winforms PropertyGrid.

When trying to set value in a variable. I want to throw an error to PropertyGrid as error that appears after putting an invalid value.

enter image description here

There is a way to do this?

有帮助吗?

解决方案

You simply throw an exception in the 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;
    }
}

Produces:

enter image description here

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top