Generic Control Validation

  • Thread starter Thread starter Richard Brown
  • Start date Start date
R

Richard Brown

Ok, I've been looking through the .NET SDK docs and stuff.

I'm wondering if you can provide a control extender that does generic
validation or functionality just by dropping it on the form. For instance,
using the IExtenderProvider interface, you can specify properties, but there
is nothing documented about linking into a control's events.

For instance, in my application, there are specific formatting,
functionality and validation for all text boxes based on a couple of
property settings. Some of these text boxes have additional validation that
must be handled per control.

So, I want all my textboxes to automatically select all text when they get
tabbed into, on leaving, I want some generic special formatting applied
according to the property setting (if the data entered is in error,
automatically set the error provider to the error text (ie, invalid date,
invalid number, etc)). Some of this functionality (say allowing the
enter-key to move between fields).

Are we able to write an Extender control to do all of this in a generic
sense, intercept or handle certain control events, and still chain to the
normal control events? This would save a *ton* of coding, basically, just
drop the extender control on a form and set some fields and your entire
application has the same 'look-feel-functionality', and a common set of code
to adjust that without having to touch everything.

Reading a little more in the documentation, the ToolTop class seems to do
this, it pops up a tooltip if you hover over the control, but I can't see
any docs on how this is actually done.
 
I'm wondering if you can provide a control extender that does generic
validation or functionality just by dropping it on the form. For instance,
using the IExtenderProvider interface, you can specify properties, but there
is nothing documented about linking into a control's events.

When the SetWhatever method is called, of your Extender class, use
AddHandler to hook into the controls events:

Public Sub SetMyProperty(ByVal ctlControl As Control, ByVal Value As String)
 
Thank you, I just happened to page into a similar example in the SDK. So
now, my next question is, does the extender interface just automatically add
"Set" and "Get" as a prefix to the property name I give in order to set and
retrieve property values? I know that this is just a sub, and I know that
it "appears" to be doing that, but just wanted a confirmation.

So, basically, adding two properties, EnterKeyNextField and SelectOnEntry
would need extender property/methods called SetEnterKeyNextField,
GetEnterKeyNextField and SetSelectOnEntry, GetSelectOnEntry. There is no
other code required to tell it that those are the two sets of property
handlers.
 
There should be an attribute attached to the Get method, and also to your
class:

<WatchForWrapping>

<ProvideProperty("Whatever", GetType(Control)) > _
Public Class MyExtenderClass
Inherits Component
Implements IExtenderProvider

<ExtenderProvidedProperty()> _
Public Function GetWhatever(ByVal ctlControl As Control) As String
Return "Foo"
End Function

Public Sub SetWhatever(ByVal ctlControl As Control, ByVal strValue As
String)
' Bar
End Sub

Public Function CanExtend(ByVal extendee As Object) As Boolean
Implements System.ComponentModel.IExtenderProvider.CanExtend

If TypeOf extendee Is Control Then
Return True
Else
Return False
End If

End Function

End Class

</WatchForWrapping>

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Ok, going the wrong way now.... I'm more confused, sorry...

The documentation says that the <ExtenderProvidedProperty()> is for support
of the .NET framework and should not be used in my code. Also, I *still* do
not see a specific statement that says "SetWhatever" is used to set the
"Whatever" property. My question was, does the .NET Framework *imply* that
if you extend a property "Whatever", you must have a method or function of
"SetWhatever" for it to work correctly? Or is there something that you
specifiy to link a specific property name to a specific handler function?
 
The documentation says that the said:
of the .NET framework and should not be used in my code.

Okay, I can live with that. I just didn't see it in the docs when I was
messing with the Extender.
My question was, does the .NET Framework *imply* that
if you extend a property "Whatever", you must have a method or function of
"SetWhatever" for it to work correctly? Or is there something that you
specifiy to link a specific property name to a specific handler function?

Basically, Yes. That's it!! The SetWhatever and GetWhatever are
automatically attached to the property ('Whatever') you are extending. It's
just like a property statement block:

Property Foo() As Bar
Get
Return Moo
End Get
Set (Value As Bar)
Moo = Value
End Set
End Property

There are actually two methods created and compiled into your application,
when compiled... get_Foo and set_Foo.

Try a bit of proof (in the IDE):

Property Foo() As Integer
Get
Return 10
End Get
Set (Value As Bar)
' Do nothing
End Set
End Property

Function get_Foo() As Integer
Return 100
End Function

The IDE wavy line's the property and tells me off, because I have a get_Foo,
which is implicitally defined by the Property.

If you think of it like that, then it's not so bad. Your SetWhatever and
GetWhatever will automatically be attached to the property you are
extending.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Back
Top