[英]TextBox not editing after SelectAll in vb.net
我有一个问题,就是我必须允许用户在文本框中输入数字值,小数点后一位
当选择了所有文本并且我尝试通过输入任何数字键来编辑文本时,它不会更改。
这是带有值的文本框。
背后的代码,按键
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
试试这个:(不是regex tho),但是非常适合数字。
它允许使用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
我已经试过这段代码:
现在可以正常工作。小数点后一位,您可以添加\\ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.