3

私は次のコードを持っています:

Private Sub TxtPStof_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPStof.KeyPress
    e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".")

End Sub

数字と。のみを許可します。ただし、テキストボックスでは、バックスペースまたは削除ボタンを使用して値を削除できる必要があります。

これは可能ですか?

ありがとう!:)

4

8 に答える 8

8

これは間違ったアプローチです。

ユーザーの入力を制限することはユーザーエクスペリエンスに悪影響を与えることは広く認められており、常に特殊なケースを処理できなくなります(たとえば、Ctrl+はどうですか?ああ、それを忘れました。誰もがそうします)。V

代わりに、.NETはユーザー入力を検証するためのValidatingイベントを提供します。キーを押すのではなく、そのイベントをインターセプトする必要があります。ユーザーが好きなようにテキストを入力できるようにしてください。特に、中断することなくミス(タイプミスなど)を行えるようにします。これは非常に混乱を招き、役に立たないでしょ

次に、それらが終了したら(入力フォーカスがコントロールを離れるため)、一度に入力検証を実行します。

于 2012-06-28T22:01:44.273 に答える
6

Konrad Rudolphからの回答には完全に同意しますが
(KeyPressイベントでのユーザー入力を処理するのは本当に面倒なことです)
、あなたの質問に答えたいと思います。

KeyPressドキュメントのMSDNには、次のように記載されていThe KeyPress event is not raised by noncharacter keysます。これは、Deleteキーではなく、BackSpaceキーのみを取得することを意味します。イベントハンドラーを少し変更するだけで、この状況に対処できます。

Private Sub TxtPStof_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPStof.KeyPress 

    if e.KeyChar <> ControlChars.Back then
        e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".") 
    end if

End Sub 
于 2012-06-28T22:43:47.423 に答える
0
If Char.IsLetterOrDigit(e.KeyChar) Or e.KeyChar = ControlChars.Back Or e.KeyChar = "." Then

        e.Handled = False

Else

        e.Handled = True

End If
于 2012-12-19T05:59:39.227 に答える
0

このコードは私にとってはうまく機能します。

 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
于 2014-01-20T08:18:22.153 に答える
0

これを試してください:

    If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
        If Not (Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46) Then e.Handled = True
    End If
于 2015-04-28T07:58:33.097 に答える
0
Private Sub textbox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textbox.KeyPress
    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
        e.Handled = True

    End If
End Sub

これはあなたがしたいことをするはずです。

于 2016-06-24T02:29:18.100 に答える
0
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim NumString = "0123456789.-"
        If Not NumString.Contains(e.KeyChar) Then
            If Asc(e.KeyChar) <> 8 Then  'BackSpace is allowed
                e.KeyChar = ""
            End If
        Else
            If e.KeyChar = "-" Then
                If Len(Trim(TxtDcrAmt.Text)) > 0 Then
                    e.KeyChar = ""
                End If
            End If

            If e.KeyChar = "." Then
                If TxtDcrAmt.Text.Contains(".") Then
                    e.KeyChar = ""
                End If
            End If

            If TxtDcrAmt.Text.Contains(".") Then
                Dim TLen As Integer = 0
                TLen = TxtDcrAmt.Text.IndexOf(".") + 2
                If Len(Trim(TxtDcrAmt.Text)) > TLen Then
                    e.KeyChar = ""
                End If
            End If
        End If
    End Sub
于 2020-09-16T06:14:34.553 に答える
-1

これをkeypressイベントに置きます。これにより、ドットと数字のみが許可されます

If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 46 Or Asc(e.KeyChar) > 57 Or (Asc(e.KeyChar) < 48 And Asc(e.KeyChar) > 46) Then
                e.Handled = True
      End If
End If
于 2017-03-02T02:51:47.493 に答える