文本框仅接受数字

发布于 2024-10-14 04:20:23 字数 399 浏览 2 评论 0原文

我找到了这段代码,使我的文本框只接受数字。

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim allowedChars As String = "0123456789"
    If allowedChars.IndexOf(e.KeyChar) = -1 Then
        ' Invalid Character
        e.Handled = True
    End If
End Sub

但是......用户无法使用退格按钮删除数字。那我该怎么办呢?

I found this code for making my textbox only accept numbers.

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim allowedChars As String = "0123456789"
    If allowedChars.IndexOf(e.KeyChar) = -1 Then
        ' Invalid Character
        e.Handled = True
    End If
End Sub

But... the user can't delete the numbers using the backspace button. How do I do then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

平生欢 2024-10-21 04:20:23
 Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles                 txtValue.KeyPress
        'Dim allowedChars As String = "0123456789"
        'If allowedChars.IndexOf(e.KeyChar) = -1 Then
        '    ' Invalid Character
        '    e.Handled = True
        'End If
        'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then
        '    e.Handled = True
        'End If
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub                                
 Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles                 txtValue.KeyPress
        'Dim allowedChars As String = "0123456789"
        'If allowedChars.IndexOf(e.KeyChar) = -1 Then
        '    ' Invalid Character
        '    e.Handled = True
        'End If
        'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then
        '    e.Handled = True
        'End If
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub                                
要走干脆点 2024-10-21 04:20:23

您还需要处理粘贴的文本(可能没有按键)。最好的方法是使用 MaskedTextBox

You also need to handle pasted text (there may not be a keypress). The best way to do this is with a MaskedTextBox.

风渺 2024-10-21 04:20:23

voldemort

我开发了您的第一个代码以允许用户也删除。

这是代码:

Dim BACKSPACE As Boolean

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Back Then
        BACKSPACE = True
    Else
        BACKSPACE = False
    End If
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If BACKSPACE = False Then
        Dim allowedChars As String = "0123456789"
        If allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End If
End Sub

我希望我的代码对您有用:)

voldemort

i develop your first code to allow the user to delete too.

Here is the code :

Dim BACKSPACE As Boolean

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Back Then
        BACKSPACE = True
    Else
        BACKSPACE = False
    End If
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If BACKSPACE = False Then
        Dim allowedChars As String = "0123456789"
        If allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End If
End Sub

I Hope My Code Was Useful To You :)

早茶月光 2024-10-21 04:20:23

使用此代码,它将对您有所帮助

Public Function OnlyDigitsOnKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

Try

    If System.Char.IsDigit(e.KeyChar) = False And e.KeyChar <> Microsoft.VisualBasic.Chr(8)        And e.KeyChar <> Microsoft.VisualBasic.Chr(46) Or (InStr(sender.text, ".") > 0 And  e.KeyChar = Microsoft.VisualBasic.Chr(46)) 
    Then
                e.Handled = True
    End If
        Catch ex As Exception
            Common.ErrorHandler(ex)
        End Try
End Function

Use this code, it will help you

Public Function OnlyDigitsOnKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

Try

    If System.Char.IsDigit(e.KeyChar) = False And e.KeyChar <> Microsoft.VisualBasic.Chr(8)        And e.KeyChar <> Microsoft.VisualBasic.Chr(46) Or (InStr(sender.text, ".") > 0 And  e.KeyChar = Microsoft.VisualBasic.Chr(46)) 
    Then
                e.Handled = True
    End If
        Catch ex As Exception
            Common.ErrorHandler(ex)
        End Try
End Function
冷情妓 2024-10-21 04:20:23

当我需要只接受数字的输入时,我通常使用 NumericUpDown 类。它也处理限制和小数。

When I have had the requirement of an input which only accepts numbers, I have typically used the NumericUpDown class. It handles limits and decimals too.

紧拥背影 2024-10-21 04:20:23
 Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress
    Dim allowedChars As String = "."
    'If allowedChars.IndexOf(e.KeyChar) = -1 Then
    '    ' Invalid Character
    '    e.Handled = True
    'End If
    'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then
    '    e.Handled = True
    'End If
    If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False And allowedChars.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
    End If
End Sub
 Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress
    Dim allowedChars As String = "."
    'If allowedChars.IndexOf(e.KeyChar) = -1 Then
    '    ' Invalid Character
    '    e.Handled = True
    'End If
    'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then
    '    e.Handled = True
    'End If
    If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False And allowedChars.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
    End If
End Sub
踏月而来 2024-10-21 04:20:23

这是我写的一些代码。它允许用户删除,并且用户可以根据需要将文本框留空。它会处理用户键入不允许的字符的情况,也会处理用户将文本粘贴到文本框中的情况。如果用户将包含有效字符和无效字符的字符串粘贴到文本框中,则有效字符将出现在文本框中,而无效字符则不会出现。

它还具有适当的逻辑来确保光标正常运行。 (将文本设置为新值的一个问题是光标移回到开头。此代码跟踪原始位置,并进行调整以考虑删除的任何无效字符。)

此代码可以放置在任何文本框的 TextChaned 事件。请务必更改 TextBox1 的名称以匹配您的文本框。

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    Dim selStart As Integer = TextBox1.SelectionStart
    Dim selMoveLeft As Integer = 0
    Dim newStr As String = "" 'Build a new string by copying each valid character from the existing string. The new string starts as blank and valid characters are added 1 at a time.

    For i As Integer = 0 To TextBox1.Text.Length - 1

        If "0123456789".IndexOf(TextBox1.Text(i)) <> -1 Then 'Characters that are in the allowed set will be added to the new string.
            newStr = newStr & TextBox1.Text(i)

        ElseIf i < selStart Then 'Characters that are not valid are removed - if these characters are before the cursor, we need to move the cursor left to account for their removal.
            selMoveLeft = selMoveLeft + 1

        End If
    Next

    TextBox1.Text = newStr 'Place the new text into the textbox.
    TextBox1.SelectionStart = selStart - selMoveLeft 'Move the cursor to the appropriate location.
End Sub

注意 - 如果您必须对一堆文本框执行此操作,您可以通过创建一个接受文本框引用作为参数的子程序来制作通用版本。然后你只需要从 TextChanged 事件中调用 sub 即可。

Here is some code that I wrote. It allows the user to delete, and the user can make the textbox blank if they desire. It handles when the user types a disallowed character, and it also handles when the user pastes text into the textbox. If the user pastes a string into the box that is a mix of valid and invalid characters, the valid characters will appear in the textbox, and the invalid characters will not.

It also has logic in place to ensure that the cursor behaves normally. (A problem with setting the text to a new value is that the cursor is moved back to the beginning. This code tracks the original position, and makes adjustments to account for any invalid characters that are removed.)

This code can be placed in the TextChaned event of any textbox. Be sure to change the name from TextBox1 to match your textbox.

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    Dim selStart As Integer = TextBox1.SelectionStart
    Dim selMoveLeft As Integer = 0
    Dim newStr As String = "" 'Build a new string by copying each valid character from the existing string. The new string starts as blank and valid characters are added 1 at a time.

    For i As Integer = 0 To TextBox1.Text.Length - 1

        If "0123456789".IndexOf(TextBox1.Text(i)) <> -1 Then 'Characters that are in the allowed set will be added to the new string.
            newStr = newStr & TextBox1.Text(i)

        ElseIf i < selStart Then 'Characters that are not valid are removed - if these characters are before the cursor, we need to move the cursor left to account for their removal.
            selMoveLeft = selMoveLeft + 1

        End If
    Next

    TextBox1.Text = newStr 'Place the new text into the textbox.
    TextBox1.SelectionStart = selStart - selMoveLeft 'Move the cursor to the appropriate location.
End Sub

Note - if you have to do this for a bunch of textboxes, you can make a general purpose version of this by creating a sub that accepts a reference to a textbox as a parameter. Then you only need to call the sub from the TextChanged event.

痴者 2024-10-21 04:20:23
Private Sub TMarksTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TMarksTextBox.KeyPress
        If e.KeyChar < "0" OrElse e.KeyChar > "9" AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
End Sub
Private Sub TMarksTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TMarksTextBox.KeyPress
        If e.KeyChar < "0" OrElse e.KeyChar > "9" AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文