Custom Date Validation Check Function

J

John Smith

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!
 
J

jayeldee

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.
 
J

John Smith

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.
 

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