I am using Winforms and CSLA 2.1.4. I have a textbox which extends the System.Windows.Forms.TextBox and adds some typical functionality when bound to a Csla object. One of them is to set the control as read only if the user cannot edit the property the control is bound to (using of course the CanWriteProperty() method). I noticed that when setting the focus inside the textbox (which currently is set to readonly) and tab out without of course making any changes, data binding tries to update my object. So a security exception is thrown but "absorbed" by databinding, thus locking the focus on the control! So the user is stuck in there and cannot even close the window.
The only workaround I can think of is to change the setter of my property as follows:
From:
set
{
CanWriteProperty("Name", true);
if (value == null) value = string.Empty;
if (_name != value)
{
_nam1 = value;
PropertyHasChanged("Name");
}
}
To:
set
{
if (value == null) value = string.Empty;
if (_name != value)
{
CanWriteProperty("Name", true);
_name = value;
PropertyHasChanged("Name");
}
}
But I don't really want to do this... Has anybody had any similar problems or can suggest a solution?
Thanks Jimbo
1. Yep I am using a BindingSource
2. That's a no... I admit that I just now read about the BindingSourceRefresh and ReadWriteAuthorizer controls which makes me feel dumb. I will definately strip that enabling/disabling functionality from my controls and use ReadWriteAuthorizer.
3. I did try change this but there was no difference. I have seen other forums suggesting this change but at the moment I have left the default .OnValidation. But surely this shouldn't make a difference?
There are various overloads for CanWriteProperty.
Most throw an exception.
I never liked this idea. And apparently now Rocky doesn't either.
I have always suggested using the overload which returns a Boolean and then branching to either return the real value or a dummy value.
Rocky now offers the same suggestion as a best practice.
The dummy value should probably be "" for string fields and 0 for numeric fields.
For example:
If CanReadProperty("Title") Then
Return mTitle
Else
Return ""
End If
Joe
That's true for reading. Returning a dummy value is as easy, or easier to debug than throwing an exception, and avoids some runtime issues that appear otherwise unavoidable.
For writing I still favor throwing an exception. Such an exception is a bug, and shouldn't happen at runtime (because you should have prevented the value from being altered by using ReadWriteAuthorizer or something similar).
The alternative with a write would be to simply and silently ignore the attempt to change the value - and that would allow hard-to-debug UI errors to exist. I think the exception is better in that case.
Copyright (c) Marimer LLC