如何在TextChanged事件中包含Keypress事件?

时间:2017-03-05 17:26:55

标签: vb.net winforms keypress textchanged

首先 - 我是学生,这是我的Capstone项目。我不想要代码作为答案,只需要一个森林指南。

我正在编写一个包含主表单和辅助表单的VB.Net应用程序。在主窗体上是用于输入信息的控件(按钮和文本框)(电子邮件和密码。换句话说,登录屏幕),我使用textchanged子来知道何时在文本框中输入了信息并尝试使用Keypress限制输入。这不是我想要的。我昨天花了很多时间研究和尝试不同的方法。只会让自己更加困惑。需要一些建议。

底线是 - 作为一个独立的子键,按键效果非常好,但按钮点击不能按预期运行。当我制作textchanged sub的按键部分时,它立即向南移动。我收到强制转换异常,但没有错误代码或其他错误指示​​。

我的首选是在textchanged子中使用按键 我希望textchenged sub监听要更改的文本框,然后由按键控制。

我做错了什么?

这个子程序没有像我认为的那样运行......

Private Sub txtemailid_TextChanged(ByVal o As [Object], ByVal e1 As KeyPressEventArgs) Handles txtemailid.TextChanged
    'Dim ac As String = "@"


    If e.KeyChar <> ChrW(Keys.Back) Then
        If Asc(e.KeyChar) < 97 Or Asc(e.KeyChar) > 122 Then
            If Asc(e.KeyChar) <> 46 And Asc(e.KeyChar) <> 95 Then
                If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                    If ac.IndexOf(e.KeyChar) = -1 Then
                        e.Handled = True
                    Else
                        If txtemailid.Text.Contains("@") And e.KeyChar = "@" Then
                            e.Handled = True
                        End If
                    End If
                End If
            End If
        End If

    End If
End Sub

以下子工作效果非常好,但会干扰窗体上的按钮控件....

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

  '*   Method   txtemailid_KeyPress()
  '*   Version:  1.00.00
  '*   Author:   Ed Petry
  '*   Date:     March-2, 2017
  '*
  '*   Description:
  '*   Intercepts the key presses as entered, allowing only acceptable characters to be entered.    
    '*    code will only allow user To input a-z(small), 0 To 9(digits), @,., 

    Dim ac As String = "@"

    If e.KeyChar <> ChrW(Keys.Back) Then
        If Asc(e.KeyChar) < 97 Or Asc(e.KeyChar) > 122 Then
            If Asc(e.KeyChar) <> 46 And Asc(e.KeyChar) <> 95 Then
                If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                    If ac.IndexOf(e.KeyChar) = -1 Then
                        e.Handled = True
                    Else
                        If txtemailid.Text.Contains("@") And e.KeyChar = "@" Then
                            e.Handled = True
                        End If
                    End If
                End If
            End If
        End If
    End If

End Sub

下面的其他说明

是的,允许使用国会大厦的字母,但教练是“好的”#39;只有要求小写的。

以下是典型的按钮子例程(我已经提交&#39;,&#39;清除表单&#39;并取消&#39;用于所述功能。此外,表单有&#39 ;创建帐户&#39;忘记密码&#39;按钮。这些按钮导致单件设计表单允许该功能(创建或恢复)。

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click


    '*   Method:   btnSubmit_Click
    '*   Version:  1.00.00
    '*   Author:   Ed Petry
    '*   Date:     February-23, 2017
    '*
    '*   Description:
    '*   Intercept user's request to submit information in attempt to login.  

    '** MAY KEEP THIS MAY NOT  Throw up a Message Box to confirm that's what the operator
    '** MAY KEEP THIS MAY NOT   wants to do.  If so, cancel the login attempt and return to the initial state.
    Try

        ' try is placeholder to verify logic is correct.

        If MsgBox("Is your information correct?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then

            ' Submit username and password informtion to business layer for verification of membership

        End If


    Catch ex As Exception
        '  An error occured!  Show the error to the user and then exit.
        MessageBox.Show(ex.Message)
    End Try

End Sub

补充评论---

这是输入信息后我正在使用的验证子。这很好..

'     uses validating event of textbox control to validate the email id using regular expression

Private Sub txtemailAddress_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtEmailAddress.Validating

    Dim pattern As String = "^[A-Z][A-Z|0-9|]*[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"

    Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(txtEmailAddress.Text.Trim(), pattern, RegexOptions.IgnoreCase)
    If (match.Success) Then
        MessageBox.Show("Success", "Checking")
    Else
        MessageBox.Show("Please enter a valid email address", "Checking")

        With txtEmailAddress
            .Clear()
            .Focus()
        End With
    End If

End Sub

0 个答案:

没有答案