dcsimg
Results 1 to 8 of 8
  1. #1

    Thread Starter
    Junior Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    17

    Resolved [RESOLVED] How to use Verification techniques on VB

    Hello guys!

    Name:  dental Record.jpg
Views: 220
Size:  27.3 KB

    I am working on a project where I want to verify each field (see image). Someone who is fillling the form can only type letters in the First and Last name textbox and for the DOB(Date of Birth) I want the user to write it in the format of DD/MM/YYYY. If this requirements are not fulfilled then an error message box should appear with the right instructions.

    1. I do not know how can I restrict the First and Last name textbox to letters only
    2. I do not know how to change DOB format to DD/MM/YYYY

    Could you please help me with the code.

    Thank You.
    Last edited by tushardevi; Dec 28th, 2017 at 04:30 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,863

    Re: [STUCK] How to use Verification techniques on VB

    For date entry do not use a Textbox unless you are forced to.

    To allow date entry it is much better to use a DateTimePicker instead, as not only does it force an appropriate format, it also ensures that only valid dates can be entered (eg: not 30th of February), allows a drop-down calendar if you want, and it allows you to directly extract the value as a Date rather than a String (which would need extra conversion and error handling if you want to store it as a Date).


    Restricting a Textbox to letters only is a bit more awkward, as you need to deal with typing and pasting etc. A fairly simple way to do it is to have code in the Changed event that removes any non-letters, but it is awkward to get it perfect.

    It is very likely that there are example(s) in our CodeBank forums of how to do it in a better way than I have described.

  3. #3
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    Re: [STUCK] How to use Verification techniques on VB

    Hi,

    like SI said use a Datetimepicker for the Date, as for Letters only
    you could use this...

    Code:
     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            Dim Result As Boolean
    
            Result = CompareText_IsLetter(TextBox1.Text)
            Select Case Result
                Case True
                    Label1.Text = Result.ToString() & " Input OK..."
                Case Else
                    Label1.Text = Result.ToString() & " Input not allowed. Remove last entry"
            End Select
        End Sub
        Private Function CompareText_IsLetter(ByVal Source As String) As Boolean
            Dim n As Integer
            For n = 0 To Source.Length - 1
                If Not Char.IsLetter(Source.Chars(n)) Then
                    Return False
                End If
            Next
            Return True
        End Function
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,527

    Re: [STUCK] How to use Verification techniques on VB

    You can also use KeyDown event to accept only keys you are interested in.

    vb.net Code:
    1. Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown
    2.         Select Case e.KeyCode
    3.             Case Keys.A To Keys.Z, Keys.Space, Keys.Delete, Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Down
    4.                 'Accept
    5.             Case Else
    6.                 e.SuppressKeyPress = True
    7.         End Select
    8.     End Sub



  5. #5

    Thread Starter
    Junior Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    17

    Re: [STUCK] How to use Verification techniques on VB

    sin_the_geek

    I found a link on this website that tells you how to restrict certain characters from a text box
    http://www.vbforums.com/showthread.p...ic-or-symbolic

    I have played around with the code and just found out that the following lines are NOT actually needed to make the code work:
    from the 1st code box:

    Dim SelectionIndex As Integer = TextBox1.SelectionStart
    Dim Change As Integer
    Change = 1
    FirstNameText.Select(SelectionIndex - Change, 0)

    It works fine without these lines! I do not know why the coder used these lines. I am pretty sure that it has a use somewhere but can't figure out actually where!

    I know TextBox1.SelectionStart workouts out the total number of letters in the string

    But I don't understand what the other lines actually mean. Like why the coder has used a variable named "change"? And what does the line FirstNameText.Select(SelectionIndex - Change, 0) actually DO!?

    Please explain why are these lines necessary, despite the fact that the code works perfectly fine without them!

    Thank you

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,527

    Re: [STUCK] How to use Verification techniques on VB

    These lines are important because they are preserve the caret position, to understand type an invalid letter, the caret will jump to the first position.



  7. #7

    Thread Starter
    Junior Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    17

    Re: [STUCK] How to use Verification techniques on VB

    Cheers everyone for helping me!

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,863

    Re: [STUCK] How to use Verification techniques on VB

    Quote Originally Posted by tushardevi View Post
    I know TextBox1.SelectionStart workouts out the total number of letters in the string
    Nope, that would be .Text.Length

    .SelectionStart tells you the start position of the Selection, and .SelectionLength tells you how long the selection is.

    Usually the Selection is 0 characters (you just see the caret, and no text selected), but you can select text with the mouse (or by pressing Shift and using the cursor keys) to see it highlighted.

    As 4x2y rightly pointed out, they (and .Select) are used in the code to make sure the caret keeps its position. It isn't the best example I've seen (because it doesn't also keep the selection if there was one), but it should be adequate.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.