![](http://vb.studysea.net/BBS/SkinImage/default/topicface/face1.gif)
在VB.NET中,通过简单的代码就可以实现对文本框的输入控制.如下图:
![按此在新窗口浏览图片](http://img1.photo.163.com/hgx32/3898905/62411291.jpg)
要想实现对三个文本框的输入按图中要求,只需对各个文本框添加KeyPress 事件,代码如下:
'TextBox1只能输入数字
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar > "0" AndAlso e.KeyChar < "9" Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
'TextBox2只能输入字母,无论大小写
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
Select Case e.KeyChar
Case "a" To "z"
e.Handled = False
Case "A" To "Z"
e.Handled = False
Case Else
e.Handled = True
End Select
End Sub
'TextBox3只能输入大写字母
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
Select Case e.KeyChar
Case "A" To "Z"
e.Handled = False
Case Else
e.Handled = True
End Select
End Sub
If (e.KeyChar >= Chr(Keys.D0) And e.KeyChar <= Chr(Keys.D9)) Or e.KeyChar = Chr(Keys.Insert) Or e.KeyChar = Chr(Keys.Space) Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(Keys.Delete) Then
e.Handled = False
Else
e.Handled = True
End If
只允许输入数字,其它的基本一样。你可以试试