【问题标题】:Masked Textbox for Double?双倍的蒙面文本框?
【发布时间】:2012-08-09 08:13:39
【问题描述】:

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

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

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

1 2 3 4 。 6 被接受为输入(包括空格),我无法删除 .我想要的任何地方。

【问题讨论】:

  • NumericUpDown 控件是首选。我知道你知道这一点,但这有点让这个问题变得无关紧要。
  • 去掉向上/向下按钮后,不喜欢 NumericUpDown 末尾的无用空间。我希望旨在接受面具的东西会有一个很好的简单答案。

标签: c# winforms maskedtextbox


【解决方案1】:

这是一个简单的解决方案。没有必要戴口罩。通常,掩码并不是所有应用程序的答案。我写了最初的解决方案。如果你想要它,你可以将它与面具结合起来。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多