textbox validation

M

markgordon_007

This should be so simple but I can not figure it out hopefully some
one can point me in the right direction...

Visual Studio 2008 and c# and winforms ....

I'm trying to create a textbox class and I want to write some generic
checks in the validate or validating method such a unique and empty
check... The control shows up in the toolbox and I can drop in on my
winforms and bind a field in a dataset to it just fine ...

The problem is when I leave the field I can not get the validate or
validating to fire when I leave the field. I set the CauseValidation
to true .... Below is a sample of the textbox class that I'm dropping
on the form ...

Any Thoughts what is going on ?

Thanks
Mark


public class BaseTextBox : TextBox
{
private void BaseTextBox_Validate()
{
MessageBox.Show("Validate");
}

private void BaseTextBox_Validating(object sender,
CancelEventArgs e)
{
bool IsValid = true
MessageBox.show("In Validating");
return IsValid;
}
}
 
M

Mark

[...]
The problem is when I leave the field I can not get the validate or
validating to fire when I leave the field. I set the CauseValidation
to true ....  Below is a sample of the textbox class that I'm dropping
on the form ...
Any Thoughts what is going on ?

Are those methods actually subscribed to the relevant events?  They aren't  
in the code you posted, and if you didn't hook them up in the Designer,  
then of course they won't be called.

A couple of asides:

     -- If you're going to sub-class the TextBox control, you may find it  
more convenient/more reliable to override the OnValidate() and  
OnValidating() methods rather than using the events.  You could write code  
to subscribe the methods explicitly in the constructor, but otherwise  
you're relying on the methods being subscribed by the Designer in  
InitializeComponent() (as a result of you or someone else hooking them up 
in the Designer).  You have to write the methods anyway...you might as  
well just make them overrides and skip the event subscription part.

     -- I would definitely _not_ use the word "Base" in the name ofa  
sub-class.  The word "Base" is so often used for base classes that using  
it in the name of a sub-class makes things look a little upside down.  :)

Pete

Thanks Pete I took your advise and just wired up the OnValidating. I
think that will work just fine
and your right it is alot easier ....

Thank Again!
Mark
 

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