【问题标题】:TextBox not editing after SelectAll in vb.net在 vb.net 中 SelectAll 后文本框不编辑
【发布时间】:2014-09-06 14:37:46
【问题描述】:

我有一个问题,问题是我必须允许用户在文本框中输入数字值,直到小数点后一位,

当所有文本被选中并且我尝试通过输入任何数字键来编辑文本时,它不会让它改变。

这是带有值的文本框。

背后的代码,Keypress

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Regex.IsMatch(TextBox1.Text, "\.\d") Then
        e.Handled = True
    End If
End Sub

请任何人提供帮助或建议更好的方法。

我已添加此代码,但我无法限制用户输入的小数点不能超过一位。

 If Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 46 Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 127) Then
        e.KeyChar = ""
        e.Handled = False
    End If

【问题讨论】:

  • 你只测试 vir 数字吗?
  • 目前我不只处理数字键,但为了测试我只输入数值。
  • 在您的代码中,您还在评估文本框中已经存在的内容,而不是从键盘“传入”的内容...不要使用 textbox1.text,使用 e 参数。
  • 为什么要重新发明轮子?使用NumericUpDown
  • 因为已经有一个TextBox来接受用户输入,所以我不能使用UpDown..control

标签: regex vb.net textbox keypress numeric


【解决方案1】:

试试这个:(不是正则表达式),但对数字很有效。

它允许 1 个负号、一个句号(点)、使用退格和任何数字。

       Private Sub priceTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles priceTextBox.KeyPress
       If IsNumeric(e.KeyChar.ToString) = False Then

            If e.KeyChar.ToString = "." And priceTextBox.Text.Contains(".") Then e.Handled = True
            If e.KeyChar.ToString = "-" And priceTextBox.Text.Contains("-") Then e.Handled = True

            If e.KeyChar.ToString <> "." Then
                If e.KeyChar <> ControlChars.Back Then
                    If e.KeyChar <> "-" Then
                        e.Handled = True
                    End If
                End If
            End If
        End If
        End Sub

编辑

这允许上述数字,以及只有 1 个小数点。我用更多的代码行来显示步骤保持简单,这样你就可以看到实际发生了什么。我相信它可以改进,这是一个快速而肮脏的版本......

  • 允许小数字符的全球化
  • 还将“粘贴”到文本框中,这通常会绕过按键捕获例程

    Dim lastProperText As String = ""
    
    Private Sub priceTextBox_TextChanged(sender As Object, e As EventArgs) Handles priceTextBox.TextChanged
    
      If priceTextBox.Text = "" Then Exit Sub
      If IsNumeric(priceTextBox.Text) = False Then priceTextBox.Text = lastProperText
      If _containsDecimal(priceTextBox.Text, 2) = True Then priceTextBox.Text = lastProperText
    
    End Sub
    
    Private Sub priceTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles priceTextBox.KeyPress
        Dim decimalSeparator As String = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
    
        If IsNumeric(e.KeyChar.ToString) = False Then
    
            If e.KeyChar.ToString = decimalSeparator And priceTextBox.Text.Contains(decimalSeparator) Then e.Handled = True
            If e.KeyChar.ToString = "-" And priceTextBox.Text.Contains("-") Then e.Handled = True
    
            'allow backspace here
            If e.KeyChar = ControlChars.Back Then Exit Sub
    
            If e.KeyChar.ToString <> decimalSeparator Then
                If e.KeyChar <> ControlChars.Back Then
                    If e.KeyChar <> "-" Then
    
                        e.Handled = True
    
                    End If
                End If
            End If
        End If
    
        'BUG FIX
        If priceTextBox.SelectionStart > priceTextBox.Text.Length - 2 Then
          If _containsDecimal(priceTextBox.Text, 1) = True Then e.Handled = True
        End If
        '/BUG FIX
    
        'keep last good format.. we will use this in case something gets apsted in that does not meet our format...
        lastProperText = priceTextBox.Text
    
    End Sub
    
    Private Function _containsDecimal(stringtoCheck As String, decimalNumber As Integer) As Boolean
    
        Dim decimalSeparator As String = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
    
        'check to allow only 1 decimal point
        Dim positionOfPoint As Integer = 0
    
        'get position of decimal point (.)
        positionOfPoint = InStr(stringtoCheck, decimalSeparator)
    
        'check if there are not characters after decimal point... 
        'if nothin after decimal point, allow this keypress, 
        'if there is already something after decimal point, escape this keypress
    
        'get length of string after "."
        Dim stringTail As String = ""
    
        If Not positionOfPoint = 0 Then
            stringTail = Mid(stringtoCheck, positionOfPoint)
            If stringTail.Length > decimalNumber Then
                Return True
            End If
        End If
    
    End Function
    

【讨论】:

  • :但它不会阻止用户只输入一位小数点值。就像我在图片中展示的那样。
  • 我已经修改了代码你能帮我弄清楚如何限制小数点后一位吗?我应该添加 TextChangedEvent 吗?
  • 请注意,此代码不支持globalization。它因我的nb-NO 文化而失败,因为小数点分隔符是, 而不是.。此外,它不会阻止用户将非数字文本粘贴到文本框中。
  • @Bjørn-RogerKringsjå ,是的,文化将是一个问题。正如我所说,它可以扩展(应该是真的)它只是一个概念证明。
  • @LouisvanTonder 太好了!您的努力值得 +1!
【解决方案2】:

我试过这段代码:

现在工作正常.. 小数点后一位,正则表达式中可以加\d\d来加位数

 Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If TextBox1.SelectedText.Length = TextBox1.Text.Length Then
        TextBox1.Clear()
    End If
    If Char.IsDigit(e.KeyChar) = True OrElse Char.IsControl(e.KeyChar) = True OrElse e.KeyChar = "."c Then
        If Regex.IsMatch(TextBox1.Text, "\.\d") Then
            'This makes backspace working
            If e.KeyChar = CChar(ChrW(8)) Then
            Else
                e.Handled = True
            End If
        End If
    Else
        e.Handled = True
    End If

End Sub

【讨论】:

  • 干净整洁。做得好。尽管请记住,您仍然可以“粘贴”非数字字符串和两位数字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多