TextBox Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hennie

    TextBox Error

    Hello,
    I've define a TextBox As Byte. Only numeric can be the input. I have made a
    ErrorCheck().
    The problem next:

    If Val(Form1.txtTe xt1.Text) "This is the problem" is not a byte Then
    etc.

    What do i have to fill in by: This is the problem is not a byte

    I have try <> Byte, but this doesn't work.
    I can the input define As String or something but I want only numeric that
    can input.

    Can someone help me, Thanks



  • James L Hill

    #2
    Re: TextBox Error

    Try this for allowing only numeric input into a text box:

    Private Sub Text1_KeyPress( KeyAscii As Integer)
    Const iNumber = "1234567890 "
    If InStr(iNumber, Chr(KeyAscii)) <> 0 Then
    Exit Sub
    Else
    KeyAscii = 0
    End If
    End Sub

    The Keypress method of the textbox is called when you type in anything.
    Setting the value of KeyAscii to 0 kills the enter.
    "hennie" <janslork@hetne t.nl> wrote in message
    news:cs8ql0$ele $1@reader08.wxs .nl...[color=blue]
    > Hello,
    > I've define a TextBox As Byte. Only numeric can be the input. I have made[/color]
    a[color=blue]
    > ErrorCheck().
    > The problem next:
    >
    > If Val(Form1.txtTe xt1.Text) "This is the problem" is not a byte Then
    > etc.
    >
    > What do i have to fill in by: This is the problem is not a byte
    >
    > I have try <> Byte, but this doesn't work.
    > I can the input define As String or something but I want only numeric that
    > can input.
    >
    > Can someone help me, Thanks
    >
    >
    >[/color]


    Comment

    • edoepke

      #3
      Re: TextBox Error

      This will allow you to useboth positive and negative numbers and decimal
      points.

      Private Sub Text1_Change()
      If Not ValidateNumeric (Text1.Text) Then
      Text1.Text = ""
      End If
      End Sub

      Private Function ValidateNumeric (strText As String) _
      As Boolean
      ValidateNumeric = CBool(strText = "" _
      Or strText = "-" _
      Or strText = "-." _
      Or strText = "." _
      Or IsNumeric(strTe xt))
      End Function

      Hope this helps.


      "hennie" <janslork@hetne t.nl> wrote in message
      news:cs8ql0$ele $1@reader08.wxs .nl...[color=blue]
      > Hello,
      > I've define a TextBox As Byte. Only numeric can be the input. I have made
      > a
      > ErrorCheck().
      > The problem next:
      >
      > If Val(Form1.txtTe xt1.Text) "This is the problem" is not a byte Then
      > etc.
      >
      > What do i have to fill in by: This is the problem is not a byte
      >
      > I have try <> Byte, but this doesn't work.
      > I can the input define As String or something but I want only numeric that
      > can input.
      >
      > Can someone help me, Thanks
      >
      >
      >[/color]


      Comment

      • Randy Birch

        #4
        Re: TextBox Error

        VBnet provides Intermediate and Advanced Win32 API code for VB developers. Comprehensive Code, FAQ, Developers Resources & News, alphabetical API/Type/Constant/Method Index, along with the largest Visual Basic-related links list on the net.


        --


        Randy Birch
        MS MVP Visual Basic



        "hennie" <janslork@hetne t.nl> wrote in message
        news:cs8ql0$ele $1@reader08.wxs .nl...
        : Hello,
        : I've define a TextBox As Byte. Only numeric can be the input. I have made
        a
        : ErrorCheck().
        : The problem next:
        :
        : If Val(Form1.txtTe xt1.Text) "This is the problem" is not a byte Then
        : etc.
        :
        : What do i have to fill in by: This is the problem is not a byte
        :
        : I have try <> Byte, but this doesn't work.
        : I can the input define As String or something but I want only numeric that
        : can input.
        :
        : Can someone help me, Thanks
        :
        :
        :

        Comment

        • Rick Rothstein

          #5
          Re: TextBox Error

          > http://vbnet.mvps.org/code/textapi/txstyles.htm

          Of course, the API function you cite for restricting input to
          digits-only suffers from the flaw that allows a user to Paste non-digits
          into the TextBox. For those reading this thread, here is a non-API
          solution that protects against Pasting too. Paste the following code
          into the code window for the form housing the TextBox (assumed to be
          named Text1 for this example).

          Rick - MVP

          Dim LastPosition As Long

          Private Sub Text1_Change()
          Static LastText As String
          Static SecondTime As Boolean
          If Not SecondTime Then
          With Text1
          If .Text Like "*[!0-9]*" Then
          Beep
          SecondTime = True
          .Text = LastText
          .SelStart = LastPosition
          Else
          LastText = .Text
          End If
          End With
          End If
          SecondTime = False
          End Sub

          Private Sub Text1_MouseDown (Button As Integer, _
          Shift As Integer, X As Single, Y As Single)
          With Text1
          LastPosition = .SelStart
          'Place any other MouseDown event code here
          End With
          End Sub

          Private Sub Text1_KeyPress( KeyAscii As Integer)
          With Text1
          LastPosition = .SelStart
          'Place any other KeyPress checking code here
          End With
          End Sub

          Comment

          Working...