By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,068 Members | 837 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,068 IT Pros & Developers. It's quick & easy.

textbox validation

P: n/a
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;
}
}

Sep 7 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sun, 07 Sep 2008 15:48:42 -0700, <ma************@yahoo.comwrote:
[...]
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 of a
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
Sep 8 '08 #2

P: n/a
On Sep 7, 8:14*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 07 Sep 2008 15:48:42 -0700, <markgordon_...@yahoo.comwrote:
[...]
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

Sep 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.