gpt4 book ai didi

c# - Double 的蒙面文本框?

转载 作者:行者123 更新时间:2023-11-30 18:38:05 25 4
gpt4 key购买 nike

我有一个蒙版文本框,我希望它能够接受 double 值。小数点前后可以容纳的字符数量不应该有种类限制。

我知道还有其他方法可以做到这一点,例如覆盖按键输入事件或使用数字上下,但我只是想问一下是否可以使用掩码。

我试过 9999999999.99999999 - 但它给了我很多问题 - 例如:

1 2 3 4 。 6个被接受为输入(包括空格)并且我无法删除 .无论我想要什么。

最佳答案

这是一个简单的解决方案。不需要面具。通常掩码并不是所有应用的答案。我写了最初的解决方案。如果需要,可以将它与面膜结合使用。

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandlerTextBoxDouble(TextBox1)
End Sub

Public Sub AddHandlerTextBoxDouble(ByVal TextBoxMe As Object)
Dim TextBox As New TextBox
TextBox = CType(TextBoxMe, TextBox)
AddHandler TextBox.KeyPress, AddressOf TextBox_KeyPress
AddHandler TextBox.TextChanged, AddressOf TextBox_TextChanged
End Sub

Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
ValidatingNumber(e, sender)
End Sub

Public Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim TextBox As New TextBox
TextBox = CType(sender, TextBox)
If Not IsNumeric(TextBox.Text) And TextBox.Text <> "." Then
TextBox.Text = ""
End If
End Sub

Public Sub ValidatingNumber(ByVal e As Object, ByVal control As Object)
Try
If CType(control, TextBox).Text.IndexOf(".") > -1 And e.KeyChar = "." Then
e.KeyChar = ""
End If
If (Asc(e.KeyChar) < 58 And Asc(e.KeyChar) > 47) Or e.KeyChar = vbBack Or e.KeyChar = "." Then
e.KeyChar = e.KeyChar
Else
e.KeyChar = ""
Try
CType(e, System.Windows.Forms.KeyPressEventArgs).Handled = True
Catch ex As Exception
End Try
End If
Catch ex As Exception
MsgBox("ValidatingNumber")
End Try

End Sub


End Class

关于c# - Double 的蒙面文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com