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

Custom Date Validation Check Function

P: n/a
Hello, I have a VB.NET application with a Windows form that have
several textboxes fields where I have dates entered. I would like to
do a date validation check after the the field is updated, so I' using
the leave event.

Right now I am creating a 'leave' sub for each of the fields.
However, I'd like to simplify that and just call the name of a
function and plug the field name as a variable and be done.

In other words, I would like to be able to pass the field name as the
variable to a function that will handle the field's leave event and
then run the validation check. If the validation check fails, set the
focus back to the field name.
This is my original code:

Private Sub OrderDate_Leave(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles OrderDate.Leave
' Code that check whether there's a valid date goes here.
End Sub

What I would like to have is:
'calling the validation date function from any part of my code
CheckforDateValidation(fieldname)

With a function that goes like this:

Public function CheckForDateValidation(ByVal fieldname As String)
That will have the event: Handles fieldname.Leave
' Code that check whether there's a valid date goes here.
' and if the validation check fails then
' set the focus back to the field that called the function.
fieldname.focus
End function
Does anyone knows how to accomplish this?

Thanks!

Mar 30 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mar 30, 9:47 am, "John Smith" <I...@NETZERO.NETwrote:
Does anyone knows how to accomplish this?

Thanks!
Using the Validating method would probably be better than the Leave as
you can set Cancel to true which will prevent the user from leaving
and you don't have to deal with any Focus code.
Make sure each Textbox has CausesValidation set to true then set up
the Handler similar to this:

Private Sub ValidateTextBoxes(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating,
TextBox2.Validating
' Cast the sender object to a Textbox
Dim txtSender As TextBox
txtSender = CType(sender, TextBox)
' If the date is not valid, set Cancel to true
e.Cancel = Not IsValidDate(txtSender.Text)
End Sub

Private Function IsValidDate(ByVal dateString As String) As
Boolean
' Perform date validation
End Function

You'll want to add each Textbox.Validating event to the Handles
statement or to make it look prettier you could do it manually in the
Form.Load with the AddHandler statement.

Mar 30 '07 #2

P: n/a
On Mar 30, 11:22 am, "jayeldee" <jayel...@gmail.comwrote:
On Mar 30, 9:47 am, "John Smith" <I...@NETZERO.NETwrote:
Does anyone knows how to accomplish this?
Thanks!

Using the Validating method would probably be better than the Leave as
you can set Cancel to true which will prevent the user from leaving
and you don't have to deal with any Focus code.
Make sure each Textbox has CausesValidation set to true then set up
the Handler similar to this:

Private Sub ValidateTextBoxes(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating,
TextBox2.Validating
' Cast the sender object to a Textbox
Dim txtSender As TextBox
txtSender = CType(sender, TextBox)
' If the date is not valid, set Cancel to true
e.Cancel = Not IsValidDate(txtSender.Text)
End Sub

Private Function IsValidDate(ByVal dateString As String) As
Boolean
' Perform date validation
End Function

You'll want to add each Textbox.Validating event to the Handles
statement or to make it look prettier you could do it manually in the
Form.Load with the AddHandler statement.
Hi Jayeldee, thanks for replying. I did as you suggested and it
worked. Thanks for all your help!

I really appreciate it.

Mar 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.