I have this evening been sitting and going through
a decompile of the System.Windows.Forms.dll.
<sitting back while waiting for abuse to be
hurled regarding intellectual property etc.>
Specifically, I have been looking at the "Data
Binding" capabilities of the controls, and why they are so problematic to use
with user defined objects. Imagine my horror,
when I realised just how simple it could be to make this usable. The "Binding"
class is what does all of the work, and this class is public too. Unfortunately,
all of the methods are private or internal (with the exception of the OnFormat()
and OnParse()) which means that I cannot override the existing functionality
through inheritance.
There is a method,
private void
Target_Validate(object sender, CancelEventArgs e) {
try
{
this.PullData();
}
catch
(Exception) {
e.Cancel = true;
}
}
If we were able to override this method in a
derived class, we could easily resolve the problems of using Data Binding the
way I keep hearing people ask ...
class NewBinding : Binding
{
// Constructors
etc.
public event CancelEventHandler
DataError;
protected override void
Target_Validating(object sender, CancelEventArgs e) {
try
{
this.PullData();
}
catch
(Exception ex) {
if (DataError != null) {
CancelEventArgs cancelEventArgs = new
CancelEventArgs(ex.Message);
DataError(sender,
cancelEventArgs);
e.Cancel =
cancelEventArgs.Cancel;
}
else
e.Cancel = true;
}
// Anything
else we do not like
}
Using this simple revision, we could use business
objects that raised Exceptions when properties were set incorrectly (just the
way Microsoft tell us to) and these objects would also be usable from Windows
Forms without having to be drastically re-written. With a little imagination, we
could also build in better support for the IDataError interface and really make
some useful bindable objects.
But we cannot. Microsoft in their infinite wisdom
have decreed that the class functionality is fixed as is.
Well, There is my whinge for the day. Anybody got
any ideas how we could get around this? I will not start up about the changes
that have taken place between versions 1.0 and 1.1 so that my existing code no
longer works anyway!
Martin Robins.