I have a Winforms PropertyGrid in c# that controls the data that getted and setted from an embedded system.
I wrote the queries about the device (GetFrequency,SetPowerLimit,SetACCurrent e.c.) as Properties so that i can bind the data without writing another method for each command inside the Form that i want to create.
The PropertyGrid works really nice, i mean it shows all the data that i need and lets me change their values. But not always of course.
Because of this black box situtation of the device that have to work on, the program halts normally at some points. (i.e. when you turn the device off.)
For example, here is a value that i have to read frequently from the device. “Simin”
[Category("Editable Values"), Description("Sets the minimum select...")]
public Ampere Simin
{
get
{...}
set
{...}
}
Within the Getters and Setters i have these get methods that establishes the connection between pc and the device. And they are the actual source of exceptions that i take mostly.
Like this
if (!_port.IsOpen)
{
throw new HuettingerException(Localisation.Error_PortClosed);
}
or this
// Read first 4 bytes
if (_port.Read(inputv, 0, 4) != 4)
{
throw new HuettingerException(Localisation.Error_NoConnection);
}
So i have three abstraction layers and i take the exceptions from the machine side (like the one shown above) while i try to read some data. I tried to catch some of them at the beginning like
public DeviceUI()
{
InitializeComponent();
try
{
propertyGrid1.SelectedObject = device;
}
catch (TimeoutException te) // i tried other exceptions too
{
MessageBox.Show(te.Message);
}
at initialization of the winform but it didn’t work.
My problem is, if it was a Button or a Textfield, i could easily catch the exceptions in the event methods, but i have no idea about handling an exception that thrown by a PropertyGrid.
Any opinions?
If the exception being thrown while converting (assigning value) (i.e. when TypeConverter.ConvertFrom() is called) to a property in the property grid (for example, InvalidCastException), then you can catch in the TypeConverter for that property.
You can also override other methods of the base TypeConverter class accordingly.
Going by the example, that you have added, it looks like you want the to deduce the reason why the communication with the device has failed (port not open, etc.). Then why not set a property or flag (for example,
ErrororCommunicationError) in theDeviceclass that tells whether the specific error has occurred.Then in the
TypeConvertercheck for this property before accessing other properties, if in error then simply return from theConvertFrom()without calling thebaseimplementation. If the error is recoverable, which in your case appears to be a communication error, then allow the user to retry, rather than throwing an exception.In addition, if you are mapping the device parameters directly onto the class and you have a property that you use in the property grid but do not want it to be written on the device, then mark it with a custom attribute (say boolean
DeviceWritable). In the communication layer (where you actually write the values to the device), check for this custom attribute, if it is set astruethen write to the device otherwise do not write to the device.