text box problems..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • obair2
    New Member
    • Jul 2006
    • 1

    text box problems..

    I am a beginner at making programs.
    I am learning visual basic bymyself . I was making a conversion program and i wanted only numericals should be entered into the textbox. Can anyone please answer in brief.
  • BSOB
    New Member
    • Jul 2006
    • 77

    #2
    i think there is a way to limit the things that are put into a text box with text1.dataforma t but what ive always used is:

    text1_lostfocus ()
    text1.text = val(text1.text)
    end sub

    ins>outs:
    k>0
    1.3>1.3
    k2>0
    3.90k>3.9
    3 2>3
    (im using k as a random letter representing any text that is not numerical)

    Comment

    • sashi
      Recognized Expert Top Contributor
      • Jun 2006
      • 1749

      #3
      Hi there,

      i would suggest to use the IsNumeric function by means to validate numeric data instead of Val function.. :)

      Code:
        If IsNumeric(text1.text) = False Then
          ..  some message here ..
        EndIf

      Comment

      • Mahesh_Bote
        New Member
        • Jul 2006
        • 26

        #4
        If IsNumeric(text1 .text) = True[U] Then
        .. some message here ..
        End If

        sashi, questioner want to enter only numeric values. so make a little bit change

        Mahesh

        Comment

        • Stampy
          New Member
          • Jul 2006
          • 3

          #5
          You may also want to try just using the maskedtextbox control instead of the textbox. This control has a mask property that you can customize to only allow numeric input, zip code/ telephone number formats, etc.

          Comment

          • sashi
            Recognized Expert Top Contributor
            • Jun 2006
            • 1749

            #6
            Hi Mahesh,

            i understood the question clearly as it sounds.. IsNumeric function will help when it comes to datatype validation.. if numeric data is entered proceed with the nextstep or else prompt an error message asking the input to be corrected.. :)

            Comment

            • BSOB
              New Member
              • Jul 2006
              • 77

              #7
              ive always tried not to bother my users with messaging telling them they are incompitant. they usualy already know that. i use val, so if they enter bad data, it goes to a default zero or null. the default value can be changed in code as desired. or, the default could be to clear the feild to let them know the data has not been set:
              if str(val(text1)) <> text1 then text1 = ""

              Comment

              • gman_00ph
                New Member
                • Jul 2006
                • 9

                #8
                this one filters the textbox from any non-numerical values:
                "If IsNumeric(text1 .text) = false then..."

                this one filters the keypress in the textbox: (you can press backspace and enter)
                Private Sub Text1_KeyPress( KeyAscii As Integer)
                If KeyAscii >= 58 Or (KeyAscii <= 47 And KeyAscii <> 8 And KeyAscii <> 13)_
                Then
                KeyAscii = 0
                End If
                End Sub

                hope that help you...

                Comment

                • JWL48
                  New Member
                  • Oct 2006
                  • 1

                  #9
                  Originally posted by Stampy
                  You may also want to try just using the maskedtextbox control instead of the textbox. This control has a mask property that you can customize to only allow numeric input, zip code/ telephone number formats, etc.
                  Great reply!!! The "normal textbox" DataFormat seems to be very weakly implemented. Why butt your head against it when the other control is begging to be used! Thanks again - and a free cold "cyber beer" to you!

                  Comment

                  Working...