2012年8月14日 星期二

控制Textbox可以輸入的英、數字

 Private Sub t_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles t.KeyPress
       
        REM 控制可輸入的字每為數字或英文字每
        REM 若反白的長度等於欄位內文字的長度,允許全部空白。
        If CType(sender, TextBox).SelectedText.Length = CType(sender, TextBox).Text.Length Then CType(sender, TextBox).Text = ""

        REM Backspace
        If e.KeyChar = Chr(8) Then e.Handled = False : Exit Sub
        REM Delete
        If e.KeyChar = Chr(46) Then e.Handled = False : Exit Sub
        REM A to Z
        If CType(sender, TextBox).Text.Length = 0 And (e.KeyChar >= Chr(65) And e.KeyChar <= Chr(90)) Then e.Handled = False : Exit Sub
        REM a to z
        If CType(sender, TextBox).Text.Length = 0 And (e.KeyChar >= Chr(97) And e.KeyChar <= Chr(122)) Then e.Handled = False : Exit Sub
        REM Zero to nine
        If CType(sender, TextBox).Text.Length >= 1 And e.KeyChar >= Chr(48) And e.KeyChar <= Chr(57) Then e.Handled = False : Exit Sub

        REM 若上述比對皆不符,也就是第一碼不等於英文,後九碼不等於數字就...不允許輸入
        e.Handled = True REM 

    End Sub

沒有留言:

張貼留言