controlling the input into a textbox

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

    controlling the input into a textbox

    Does anyone know how can ensure that the only input that is accepted into a textbox is a numeric (only digits 1-9 accepted)

    Is this possible

    I am using vb.net to create a windows app

    thx folks
  • Cor

    #2
    Re: controlling the input into a textbox

    Hi Massy,

    1-9 or 0 to 9?

    Cor


    Comment

    • Cor

      #3
      Re: controlling the input into a textbox

      Hi Massy,

      Does not matter, this is a sample as you was asking.

      Try it, I hope this helps?

      Cor

      Private Sub textbox1_KeyUp( ByVal sender As Object, _
      ByVal e As System.Windows. Forms.KeyEventA rgs) Handles textbox1.KeyUp
      If e.KeyValue <> 8 Then
      If Not IsNumeric(textb ox1.Text) OrElse CInt(textbox1.T ext) = 0
      MessageBox.Show ("Only 1 to 9 is allowed")
      textbox1.Focus( )
      End If
      End If
      End Sub


      Comment

      • Rigga

        #4
        Re: controlling the input into a textbox

        Private Sub TextBox_KeyPres s(ByVal sender As Object, ByVal e As
        System.Windows. Forms.KeyPressE ventArgs) Handles MyBase.KeyPress
        If Char.IsControl( e.KeyChar) Then
        Exit Sub
        End If
        If Not Char.IsNumber(e .KeyChar) Then
        e.Handled = True
        End If
        End Sub


        HTH
        Rigga.

        "Massy" <anonymous@disc ussions.microso ft.com> wrote in message
        news:2063BDDF-FD62-413F-B16D-4828EA5A92A0@mi crosoft.com...[color=blue]
        > Does anyone know how can ensure that the only input that is accepted into[/color]
        a textbox is a numeric (only digits 1-9 accepted)?[color=blue]
        >
        > Is this possible?
        >
        > I am using vb.net to create a windows app.
        >
        > thx folks[/color]


        Comment

        • Massy

          #5
          Re: controlling the input into a textbox

          Hi Cor

          I tried your code and it worked fine. However it still leaves the character that has been pressed to remain in the textbox after the messagebox has been displayed. Is there anyway to remove the character that has just been input?

          Rigga
          I tried the example u gave me but i could not get it to work. Is there something im doin wrong
          I replaced the textbox1 value in your code to the name of my textbox. Even i the user enters charcter values in this field, no action is taken! Is there something else i should be doin

          thx

          Comment

          • Massy

            #6
            Re: controlling the input into a textbox

            Hi Rigga, I have just got your code to work, I changed the mybase.Keypress to MyTextbox.Keypr ess

            Thx for your code Cor and Rigga. It is appreciated
            :o)

            Comment

            Working...