【解決方法】vb.netを使用してWindowsアプリケーションのテキストボックスに英数字のみを許可する方法


こんにちは、私は Windows アプリケーションの vb.net に要件があります。 テキストボックスは英数字のみを許可し、最初の文字は数字ではなく文字のみにする必要があります。 特殊文字、記号、下線、スペースは使用できません。 他の文字が入力された場合は、検証メッセージが表示されます。

誰か助けてください。

解決策 1

まず、質問に対する私のコメントをご覧ください。

イベントを処理する入力から不要な文字を簡単に除外できます KeyPress または仮想メソッドをオーバーライドする OnKeyPress:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress.aspx[^]、
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx[^].

このイベントには、上記の 2 番目の記事の参照で説明されているキャンセル メカニズムがあります。 以下も参照してください。 http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled.aspx[^].

プロパティの値にtrueを代入すると Handled イベント引数のうち、イベントをキャンセルすると、不要な文字が無視されます。 さて、「英数字」について。 次の述語を確認できます。
http://msdn.microsoft.com/en-us/library/system.char.isletter.aspx[^]、
http://msdn.microsoft.com/en-us/library/system.char.isdigit.aspx[^].

あなたがアイデアを得たことを願っています。

そして 1 つの重要なポイント: バックスペースを許可することを忘れないでください。 いくつかの奇妙な歴史的理由により、この hey はコード ポイント #8 を持つ「文字」と見なされます。 他の不要な文字で除外すると、編集コントロール (テキスト ボックスなど) で前の文字を削除できなくなります。

-SA

解決策 2

VB
' for example i allow "." and "," but if you not need then just remove those condition.
Function Isnumber(ByVal KCode As String) As Boolean
        Isnumber = True
        If Not IsNumeric(KCode) And KCode <> ChrW(Keys.Back) And KCode <> ChrW(Keys.Enter) And KCode <> "."c And KCode <> ","c Then
            Isnumber = False
            MsgBox("Please Enter Numbers only", MsgBoxStyle.OkOnly)
        End If

    End Function
VB
' put this code under textbox keypress event
  Private Sub textbox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmount.KeyPress
        If Not Isnumber(e.KeyChar) Then
            e.KeyChar = ""
        End If
    End Sub

解決策 4

VB
' Boolean flag used to determine when a character other than a number is entered.
    Private nonNumberEntered As Boolean = False

    ' Handle the KeyDown event to determine the type of character entered into the control.
    Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
         Handles TextBox1.KeyDown
        ' Initialize the flag to false.
        nonNumberEntered = False

        ' Determine whether the keystroke is a number from the top of the keyboard.
        If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
            ' Determine whether the keystroke is a number from the keypad.
            If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                ' Determine whether the keystroke is a backspace.
                If e.KeyCode <> Keys.Back Then
                    ' A non-numerical keystroke was pressed. 
                    ' Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = True
                End If
            End If
        End If
    End Sub 'textBox1_KeyDown


    ' This event occurs after the KeyDown event and can be used 
    ' to prevent characters from entering the control.
    Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles TextBox1.KeyPress
        ' Check for the flag being set in the KeyDown event.
        If nonNumberEntered = True Then
            ' Stop the character from being entered into the control since it is non-numerical.
            e.Handled = True
        End If
    End Sub 'textBox1_KeyPress

コメント

タイトルとURLをコピーしました