Custom Control - how to call another class's validation method

N

Neville Lang

I have built a custom control where I now combine the features of both a
ComboBox and TextBox for the Compact Framework.

One of the features of the TextBox is to validate user input on the TextBox
component when it loses focus. Since this custom control can be used by a
number of different classes, each class is likely to have a different
validation method name. How can I pass the validation method's name to my
custom control so that it can run that method inside the custom control when
the TextBox component of my custom control loses focus?

Further, my TextBox's LostFocus event already calls a event handler inside
my custom control to do some other things so I want to be able to call the
external validation method from inside that event handler.

Regards,
Neville Lang
 
S

Sergey Bogdanov

Actually, this is not Compact Framework question, but more OOP question...

In this case I would recomend you to consider Strategy Pattern [1] :-
provide ValidationAlgorith property for your control:

IValidation _validator = new DefaultValidator();
IValidation ValidationAlgorithm
{
set
{
_validator = value;
}
}


Then define IValidation interface in this manner:

interface IValidation
{
bool Validate(string text);
}

On LostFocus you have to write something like this:

protected override void OnLostFocus(...)
{
if (!_validator.Validate(Text)) return; // or do some action
...
}

Then somewhere in your form dynamically you may substitute validation
algorithm in this way:

yourControl.ValidationAlgorithm = new NewValidator();


HTH

[1] http://www.dofactory.com/Patterns/PatternStrategy.aspx
 
N

Neville Lang

Sergey,

Yes, you are right - this was an OOP query and I had a mental block while
trying to work out a solution here. Thank you for your Interface solution -
it works perfectly.

Thanks also for your link to the Patterns page - I found it helpful and have
bookmarked that link if for future OOP design issues.

Regards,
Neville Lang



Sergey Bogdanov said:
Actually, this is not Compact Framework question, but more OOP question...

In this case I would recomend you to consider Strategy Pattern [1] :-
provide ValidationAlgorith property for your control:

IValidation _validator = new DefaultValidator();
IValidation ValidationAlgorithm
{
set
{
_validator = value;
} }


Then define IValidation interface in this manner:

interface IValidation
{
bool Validate(string text);
}

On LostFocus you have to write something like this:

protected override void OnLostFocus(...)
{
if (!_validator.Validate(Text)) return; // or do some action
...
}

Then somewhere in your form dynamically you may substitute validation
algorithm in this way:

yourControl.ValidationAlgorithm = new NewValidator();


HTH

[1] http://www.dofactory.com/Patterns/PatternStrategy.aspx

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Neville said:
I have built a custom control where I now combine the features of both a
ComboBox and TextBox for the Compact Framework.

One of the features of the TextBox is to validate user input on the
TextBox component when it loses focus. Since this custom control can be
used by a number of different classes, each class is likely to have a
different validation method name. How can I pass the validation method's
name to my custom control so that it can run that method inside the
custom control when the TextBox component of my custom control loses
focus?

Further, my TextBox's LostFocus event already calls a event handler
inside my custom control to do some other things so I want to be able to
call the external validation method from inside that event handler.

Regards,
Neville Lang
 

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