0

我知道以下代码阻止用户在文本框中使用空格但是我如何允许用户只使用数字和句号(所以我可以添加像 1.5 这样的值)

    Private Sub Textbox4_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox4.KeyDown

    If e.KeyCode = Keys.Space Then
        TextBox4.Clear()
        MsgBox("Invalid character. No spaces Permited...")

    End If
4

4 回答 4

2

From a usability point of view, testing for valid input in the KeyDown event isn’t good. For example, what happens when the user wants to paste text into your text box?

Furthmore, the user can still paste invalid input using the TextBox’ context menu, your code won’t notice this.

You should allow all input, and then test for validity when the user leaves the text box. VB has an extra event for this, which is fired whenever the text box loses focus: Validating. Use this event (and only this event) to test for valid input.

于 2011-02-17T15:23:04.267 回答
0

一个简单的方法可能是查找“允许的”字符,如果不是其中一个,则显示错误消息。

于 2011-02-17T15:20:43.030 回答
0

在过去 20 年的代码编写中,我总是使用以下基本原理来检查 TextBoxes Check Characters。
首先,您必须创建一个单独的类,您可以将其称为(为方便起见)Char_Validation。
在这个类中,您将放置一个返回布尔值的函数,如下所示。

Public Class Char_Validation
    Public Const Gr As String = "Greek"
    Public Const En As String = "English"
    Public Const Num As String = "Numbers"
    Public Const FullGr As String = "Full Greek"
    Public Const FullEn As String = "Full English"
    Public Const EnN As String = "English with Numbers"
    Public Const GrN As String = "Greek with Numbers"

    Public Shared Function ValidateChar(ByVal AsciiChar As String, ByVal CharTable As String, ByVal sender As Object, ByVal e As System.EventArgs) As Boolean
        Dim ConvChar As Integer = CUInt(Microsoft.VisualBasic.Asc(AsciiChar))
        Dim ConvCharW As Integer = CUInt(Microsoft.VisualBasic.AscW(AsciiChar))

        ValidateChar = False

        Select Case CharTable
            Case En
                Select Case ConvChar
                    Case 65 To 126, 145 To 150, 8, 32 To 47, 58 To 64, 128, 130
                        ValidateChar = True
                End Select
            Case EnN
                Select Case ConvChar
                    Case 48 To 57, 65 To 126, 8, 32, 45
                        ValidateChar = True
                End Select
.
.
.
.
.
            Case Num
                Select Case ConvChar
                    Case 44 To 57, 92, 8
                        ValidateChar = True
                End Select
        End Select

    End Function
End Class

在您的 Class in Form 中,您将使用 TextBox_KeyPress,您将在其上使用以下代码。

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        ErrorProvider1.Clear()
        ErrorLabel.ForeColor = Drawing.Color.Black
    Select Case Char_Validation.ValidateChar(e.KeyChar, Char_Validation.Num, sender, e)
            Case True
            Case False
                ErrorProvider1.SetError(TextBox1, "Wrong Character Only Numbers")
                Beep()
    e.KeyChar = ""
        End Select
    End Sub

因此,您将禁止用户将字符置于您的决定之外。
我希望这将从现在开始涵盖您。

于 2011-02-18T19:13:06.190 回答
0

以下代码适用于我:firefox、IE 8、chrome、Safari 和 iphone。

函数 dotplaced(myfield){ if(myfield.indexOf(".")===-1){ return false; } 返回真;
} 函数 NumbersOnly(myfield, e) { var key; 变量键字符;

if (window.event) {
    key = window.event.keyCode;
}
else if (e) {
    key = e.which;
}
else {
    return true;
}

keychar = String.fromCharCode(key);

// control keys
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27)) {
    return true;
}
// numbers
else if ((("0123456789").indexOf(keychar) > -1)) {
    return true;
}
// decimal point jump
else if (!dotplaced(myfield.value) && (keychar == ".")) {
    //myfield.form.elements[dec].focus();        
    return true;
}
else {
    return false;
}

}

于 2011-04-15T13:17:47.977 回答