?
Solved

User input validation VB.Net

Posted on 2007-03-28
3
Medium Priority
?
816 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
[X]
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
3 Comments
 
LVL 25

Assisted Solution

by:SStory
SStory earned 200 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 1800 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 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