Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

User input validation VB.Net

Posted on 2007-03-28
3
800 Views
Last Modified: 2012-06-22
Hopefully from the below people can understand what i am trying to do.....

I have a masked text box array created at runtime (00:00 mask) and wish to make sure that time entered is valid. I have been racking my brains at this all day and have not come up with a solution perhaps experts here will be able to stop me from going completely crazy. I have the below code but when i run the application i am still able to enter an invalid time.

Blocksoff(z) = New MaskedTextBox
        With Blocksoff(z)
            .Left = 355
            .Top = 52 + (z * 25)
            .Height = 20
            .Width = 35
            .Mask = TMask
            .ValidatingType = GetType(System.DateTime)
        End With
        Me.Controls.Add(Blocksoff(z))

Any help with input validation would be much appreciated.

Thanks in advance
0
Comment
Question by:dr_dudd
3 Comments
 
LVL 25

Assisted Solution

by:SStory
SStory earned 50 total points
ID: 18811478
How about in the TextChange event, validating the text and if it isn't a valid time, clear out the text, beep or whatever and tell the user?

Basically when it is all said and done you will have a string.
You can parse that string .Substring method for each piece and see if it is in range. The first 1 or 2 digits, should be between 0 and 12 or 0 and 23 for the hour depending on military time or not. the next pairs of digits must be 0 to 60.  This shouldn't be too hard to write.

Another way to do it is in the KeyPress event, each time they press a key  you decide if the input is acceptable and if not you disallow that keypress (a little more complicated to write, but still not too bad).
0
 
LVL 34

Accepted Solution

by:
Sancler earned 450 total points
ID: 18812346
The code you show is not enough, in itself, to stop invalid entries.  Validation works via the .TypeValidationCompleted event, and then you have to code within the sub for that for how to react to invalid data.  So you need two things.  One is a sub containing your "reaction code".  The other is an event handler added to you control to raise the event that will call that sub.  At its simplest you could add a sub like this

    Private Sub CheckValidation(ByVal sender As Object, ByVal e As System.Windows.Forms.TypeValidationEventArgs)
        If Not e.IsValidInput Then
            MsgBox("Not a valid time")
            e.Cancel = True
        End If
    End Sub

and then add the following line in your With block, perhaps here

            .ValidatingType = GetType(System.DateTime)
             AddHandler .TypeValidationCompleted, AddressOf CheckValidation '<<< NEW LINE ADDED

Roger
0
 
LVL 2

Author Comment

by:dr_dudd
ID: 18814344
I have split the points as both are valid solutions. I have tried both and they both work. Many thanks experts :-)

I have used the second solution and given more points as it is a more complete answer with code.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - MemoryMappedFiles - Confirm receipt 2 24
VB.NET 2008 Winforms Signing 13 30
Vb.net threads keep increasing 2 35
Code enhancement 4 20
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question