Limiting Text Input 
Author Message
 Limiting Text Input

Can anyone tell me a quick and efficient way that I can limit the type of
text that a user can input into a textbox as being positive integers only? I
would like to prevent someone from entering letters or the negative sign
into a textbox at all, as opposed to checking for it and producing an error
message after the information is submitted.

thanks for your help,

ron



Sun, 30 Jan 2005 20:22:00 GMT  
 Limiting Text Input
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles _
TextBox1.KeyPress
   If Not e.KeyChar.IsDigit(e.KeyChar) Then ' use IsNumber if you want to
allow hex
      e.Handled = True
   End If
End Sub

Add the minus key in there and that should handle it.


Quote:
> Can anyone tell me a quick and efficient way that I can limit the type of
> text that a user can input into a textbox as being positive integers only?
I
> would like to prevent someone from entering letters or the negative sign
> into a textbox at all, as opposed to checking for it and producing an
error
> message after the information is submitted.

> thanks for your help,

> ron



Sun, 30 Jan 2005 21:15:21 GMT  
 Limiting Text Input
Gracias mi amigo.


Quote:
> Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
> System.Windows.Forms.KeyPressEventArgs) Handles _
> TextBox1.KeyPress
>    If Not e.KeyChar.IsDigit(e.KeyChar) Then ' use IsNumber if you want to
> allow hex
>       e.Handled = True
>    End If
> End Sub

> Add the minus key in there and that should handle it.



> > Can anyone tell me a quick and efficient way that I can limit the type
of
> > text that a user can input into a textbox as being positive integers
only?
> I
> > would like to prevent someone from entering letters or the negative sign
> > into a textbox at all, as opposed to checking for it and producing an
> error
> > message after the information is submitted.

> > thanks for your help,

> > ron



Sun, 30 Jan 2005 21:34:42 GMT  
 Limiting Text Input
You could try the following code :

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
    Dim sText As String = ""
    Dim iIndex As Int16
    Dim iPos As Int16 = TextBox1.SelectionStart

    For iIndex = 0 To TextBox1.TextLength - 1
        If Char.IsDigit(TextBox1.Text.Chars(iIndex)) Then
            sText = sText & TextBox1.Text.Chars(iIndex)
        End If
    Next

    TextBox1.Text = sText
    TextBox1.SelectionStart = iPos
End Sub

The problem with the KeyPress event is that if someone rightclicks your
textbox and paste something in it you won't detect it.  Using this function
your certain everything will be detected.  The bad thing is, everytime all
chars in the textbox need to be checked (since you have no idea if it was a
pasting and if so, where it took place.

HTH

Yves



Quote:
> Can anyone tell me a quick and efficient way that I can limit the type of
> text that a user can input into a textbox as being positive integers only?
I
> would like to prevent someone from entering letters or the negative sign
> into a textbox at all, as opposed to checking for it and producing an
error
> message after the information is submitted.

> thanks for your help,

> ron



Sun, 30 Jan 2005 22:32:43 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. dbgrid limit text input length?

2. Text area input width limit

3. How to limit input into a text box

4. Rich Text Box - Limiting Input

5. Text edit control that limits input to certain chars

6. Input parameter limit?

7. Limiting data input through relations?

8. Limiting user input in a Textbox

9. limit a user-input into a combo-box

10. Limiting input in an array

11. Input File Size Limit???

12. Textbox - Limiting Input to Numbers, decimal place and negative sign

 

 
Powered by phpBB® Forum Software