Pergunta

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?

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top