Solved

User input validation VB.Net

Posted on 2007-03-28
3
797 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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