C# - How to raise custom Validation Message in Property Grids?

S

Shanthi

I am using property grid in C# application. Property grid displays the
propeties properly. But I have some problem in displaying custom validation
message.
I have integer type property. When I give invalid values for the property it
is throwing System exception as follows.

"Invalid Property Value. Is not a valid value for Int32.

Though I have custom validation message in the Property set system throws
exception before this.

Is there any way to override system defined exception?

How can I avoid this?

Can anyone help in this?

Thanks in advance.

Regards
Shanthi
 
B

Ben Rush

I'm not sure as though I understand.

You mean you have a property of your type, say, "prop" that is of type
System.Int32, and so is used like thus -

........
MyObj.prop = 4;
........

and you want to override exceptions you get when you try to set it to
something invalid, like thus -

........
MyObj.prop = "blah";
........

My first assumption would be, no, you can't and shouldn't override the
default exceptions you'd get back. My feelings are that if I could
circumvent the exceptions, somehow, that would get generated by
mis-assignment of types that would be a major flaw in the type-safety of the
..Net runtime itself.

Remember that properties are functions. And what you're trying to do is,
from within the function, catch an invalid parameter value AFTER it has
already been constructed on the stack-frame for that function, type-checked
and so forth. I don't see how this is possible.
 
S

Shanthi

Thanks for the reply.

I agree with you that overriding the system exception may not be a good
idea.
What I need is to validate the inputs to the propety grid.
I am trying to resolve this problem from another angle now.
Is it possible to validate key strokes while the user enters so that I can
disallow chars where the fields need to be numeral. Looking for some key
board / data change events on the property grid.


Thanks & Regards
Shanthi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top