Custom Date Validation Check Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    #1

    Custom Date Validation Check Function

    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.EventArg s) 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
    CheckforDateVal idation(fieldna me)

    With a function that goes like this:

    Public function CheckForDateVal idation(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!

  • jayeldee

    #2
    Re: Custom Date Validation Check Function

    On Mar 30, 9:47 am, "John Smith" <I...@NETZERO.N ETwrote:
    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 CausesValidatio n set to true then set up
    the Handler similar to this:

    Private Sub ValidateTextBox es(ByVal sender As Object, ByVal e As
    System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting,
    TextBox2.Valida ting
    ' 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(txt Sender.Text)
    End Sub

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

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

    Comment

    • John Smith

      #3
      Re: Custom Date Validation Check Function

      On Mar 30, 11:22 am, "jayeldee" <jayel...@gmail .comwrote:
      On Mar 30, 9:47 am, "John Smith" <I...@NETZERO.N ETwrote:
      >
      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 CausesValidatio n set to true then set up
      the Handler similar to this:
      >
      Private Sub ValidateTextBox es(ByVal sender As Object, ByVal e As
      System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting,
      TextBox2.Valida ting
      ' 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(txt Sender.Text)
      End Sub
      >
      Private Function IsValidDate(ByV al dateString As String) As
      Boolean
      ' Perform date validation
      End Function
      >
      You'll want to add each Textbox.Validat ing 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.

      Comment

      Working...