-7

我只想输入数字,不允许非数字,请帮助我。

此代码在 TextBox 但它不适用于 PasswordBox

        Dim textBox As TextBox = TryCast(sender, TextBox)
        Dim selectionStart As Int32 = textBox.SelectionStart
        Dim selectionLength As Int32 = textBox.SelectionLength
        Dim newText As [String] = [String].Empty
        Dim count As Integer = 0
        For Each c As [Char] In textBox.Text.ToCharArray()
            If [Char].IsDigit(c) OrElse [Char].IsControl(c) Then
                newText += c
            End If
        Next
        textBox.Text = newText
        textBox.SelectionStart = If(selectionStart <= textBox.Text.Length, selectionStart, textBox.Text.Length)

谢谢你

4

1 回答 1

0
Private Sub PbPhone_PreviewTextInput(sender As Object, e As TextCompositionEventArgs) Handles PbPhone.PreviewTextInput
    Dim regex As New Regex("[0-9]+")
    If Not regex.IsMatch(e.Text) Then
        e.Handled = True
    End If
End Sub
于 2016-08-16T06:27:34.270 回答