PC Review


Reply
Thread Tools Rate Thread

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

 
 
Neville Lang
Guest
Posts: n/a
 
      18th Aug 2005
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


 
Reply With Quote
 
 
 
 
Sergey Bogdanov
Guest
Posts: n/a
 
      18th Aug 2005
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 Lang wrote:
> 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
>
>

 
Reply With Quote
 
Neville Lang
Guest
Posts: n/a
 
      18th Aug 2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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 Lang wrote:
>> 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
>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived class Interface method needs to call Base Class Interface Method Kevin Frey Microsoft C# .NET 2 1st Dec 2006 06:57 PM
call a mnuFileNew_Click method from a Main MDI class from another class (can be a MdiChild class.. )? M. G, Microsoft Dot NET Framework Forms 1 31st May 2006 06:28 AM
It's not possible to call method in a class library dll from a user control dll Tony Johansson Microsoft Dot NET 4 3rd Apr 2006 11:19 AM
Why is it not possible to call a class library method from a user control. Tony Johansson Microsoft C# .NET 5 3rd Apr 2006 02:52 AM
Is it possible to call a static method in the type class of a generic class? steve bull Microsoft C# .NET 10 8th Dec 2005 09:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:54 AM.