vb.net中限制只输入小写字母
PublicClassForm1PrivateSubTextBox1_KeyPress(ByValsenderAsObject,ByValeAsSystem.Window...
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim keyascii As Integer
If keyascii > 122 Or keyascii < 97 Then
keyascii = 0
End If
End Sub
End Class 展开
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim keyascii As Integer
If keyascii > 122 Or keyascii < 97 Then
keyascii = 0
End If
End Sub
End Class 展开
3个回答
2013-08-08
展开全部
不用这个应该使用重载Form的ProcessCmdKey,假如我们处理的是textBox1如下: Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If Not TextBox1.Focused Then Return False '焦点不在指定文本框上,不处理
If keyData >= Keys.A And keyData <= Keys.Z Then '是字母,还不知道是否是大小写
If IsKeyLocked(Keys.CapsLock) Then '大写锁定
Return Not CInt(ModifierKeys()) And CInt(Keys.Shift) <> 0 '如果Shift没锁定(即是大写)要过滤,返回TRUE;
Else
Return CInt(ModifierKeys()) And CInt(Keys.Shift) <> 0 '如果Shift锁定(即是大写)要过滤,返回TRUE;
End If
ElseIf keyData = Keys.Back Or keyData = Keys.Delete Then '还有允许的其它键
Return False
End If
Return True '剩余的键都过滤掉
End Function
If Not TextBox1.Focused Then Return False '焦点不在指定文本框上,不处理
If keyData >= Keys.A And keyData <= Keys.Z Then '是字母,还不知道是否是大小写
If IsKeyLocked(Keys.CapsLock) Then '大写锁定
Return Not CInt(ModifierKeys()) And CInt(Keys.Shift) <> 0 '如果Shift没锁定(即是大写)要过滤,返回TRUE;
Else
Return CInt(ModifierKeys()) And CInt(Keys.Shift) <> 0 '如果Shift锁定(即是大写)要过滤,返回TRUE;
End If
ElseIf keyData = Keys.Back Or keyData = Keys.Delete Then '还有允许的其它键
Return False
End If
Return True '剩余的键都过滤掉
End Function
2013-08-08
展开全部
可以使用keydown事件 Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode < Keys.A + 32 Or e.KeyCode > Keys.Z + 32 Then '如果不是小写
If e.KeyCode <> Keys.Delete And e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Right And e.KeyCode <> Keys.Left Then' 还是不Del,Backspace,也不是左右箭头,
e.Handled = True 把这个属性置为True就取消了这次按键
End If
End If
End Sub
If e.KeyCode < Keys.A + 32 Or e.KeyCode > Keys.Z + 32 Then '如果不是小写
If e.KeyCode <> Keys.Delete And e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Right And e.KeyCode <> Keys.Left Then' 还是不Del,Backspace,也不是左右箭头,
e.Handled = True 把这个属性置为True就取消了这次按键
End If
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-08
展开全部
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
select case e.keycharcase "a" to "z"case chr(8) 'Backspace Keycase else e.handled=trueend selectEnd Sub
select case e.keycharcase "a" to "z"case chr(8) 'Backspace Keycase else e.handled=trueend selectEnd Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询