当只允许字母时,如何用两个不同的文本框确定是否输入数字?

时间:2019-02-07 03:36:29

标签: vb.net visual-studio visual-studio-2015

仅允许在“ txtfirstname”和“ txtlastname”中输入字母。这两个事件都在一个例外下处理(每个教授),我不知道对两个事件分别使用什么声明。它的工作图像不存在(txtFirstName或Else txtLastName,“只能是字母”)。我可以使名字正常工作,但不能使名字和姓氏同时工作。

Only working for First name

 Private Sub OnlyLetter_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtFirstName.KeyPress, txtLastName.KeyPress
        'prevents numbers from being entered into First name or Last name textbox
        If Char.IsControl(e.KeyChar) Then Exit Sub
        If Not Char.IsLetter(e.KeyChar) Then
            e.Handled = True
            errorProvider.SetError(txtFirstName OrElse txtLastName, "Can only be letters")
        Else
            errorProvider.SetError(txtFirstName, String.Empty)
        End If

1 个答案:

答案 0 :(得分:0)

您可以使用它。

 Private Sub OnlyLetter_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtFirstName.KeyPress, txtLastName.KeyPress
    If Char.IsControl(e.KeyChar) Then Exit Sub
    If Not Char.IsLetter(e.KeyChar) Then
        e.Handled = True
        errorProvider.SetError(DirectCast(sender, Textbox) ,"Can only be letters")
    Else
        errorProvider.SetError(DirectCast(sender, Textbox) , String.Empty)
    End If
 End Sub