Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top