【学生】vb中的各种限制

【前言】

在学生信息管理系统中会遇到输入姓名、学号、联系电话和日期的时候,这个时候为了方便使用,往往是需要限制一下文本框的输入信息的。

【正文】

  • 限制文本框只能输入数字和删除键
Private Sub txtClassno_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 48 To 57
        Case 8
    Case Else
        KeyAscii = 0
        MsgBox "请输入数字", vbOKOnly + vbExclamation, "提示"
        txtClassno.Text = ""
    End Select
End Sub
  • 限制文本框只能输入汉字和删除键
Private Sub txtDirector_KeyPress(KeyAscii As Integer)
    If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Then
    Else
        KeyAscii = 0
        MsgBox "请输入汉字!", vbOKOnly + vbExclamation, "提示"
        txtDirector.SetFocus
    End If
End Sub

  • 限制输入的数字长度
If Len(txtTel.Text) <> 11 Then
    MsgBox "请输入11位数字电话号码", vbOKOnly + vbExclamation, "警告"
    txtTel.Text = ""
    txtTel.SetFocus
    Exit Sub
End If
  • 禁止复制
'禁止单击右键
Private Sub txtName_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = MouseButtonConstants.vbRightButton Then
    MsgBox "右键菜单被禁用.", vbOKOnly, "对不起"
    End If
End Sub
  •  禁止使用剪切板
'清空剪切板
Private Sub txtCardNo_KeyDown(KeyCode As Integer, Shift As Integer)
    If Shift = 2 And KeyCode = vbKeyV Then  '如果是Ctrl+V,清空剪贴板
        Clipboard.Clear
    End If
End Sub

 '清空剪切板
Private Sub txtCardNo_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 2 Then      '如果是右键,清空剪贴板
        Clipboard.Clear
    End If
End Sub
  • 限制文本框的内容不为空
'限制文本框输入内容不为空
If Not Testtxt(txtClassno.Text) Then
     MsgBox "请输入班号!", vbOKOnly + vbExclamation, "警告"
     txtClassno.SetFocus
     Exit Sub
End If
  •  判断文本框是否输入数字
'判断输入文本框是否输入数字
If Not IsNumeric(Trim(txtSID.Text)) Then
    MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告"
    Exit Sub
    txtSID.SetFocus
End If
  •  限制成绩的取值范围是0-120
'限制成绩的取值范围是0-120
Private Sub txtResult_Change()
On Error Resume Next
If Val(Trim(txtResult.Text)) > 120 Or (Trim(txtResult.Text)) < 0 Then
    MsgBox "输入数字超出范围,请重新输入"
    txtResult.Text = ""
    txtResult.SetFocus
End If
End Sub
  •  限制用户名只能输入数字和字母
'限制用户名只能输入数字和字母
Private Sub txtUsername_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 48 To 57  '只能输入数字
        Case 65 To 90  '只能输入大小写字母
        Case 97 To 122
        Case 8 ' 只能输入退格
        Case Else
        KeyAscii = 0  '不能输入空格
        End Select
End Sub
  • 只能输入汉字和数字
If ((KeyAscii <= 57 And KeyAscii >= 48) Or (KeyAscii <= -3652 And KeyAscii >= -20319) Or KeyAscii = 8) = False Then KeyAscii = 0

  •  只能输入数字和英文字母
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If ((KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122)) = False Then KeyAscii = 0
End Sub
  • 只能输入汉字和英文字母
Private Sub txtName_Change()
    txtName.MaxLength = 10  '限制长度为10
End Sub
Private Sub txtName_KeyPress(KeyAscii As Integer)
     If ((KeyAscii <= -3652 And KeyAscii >= -20319) Or (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Or KeyAscii = 32 Or KeyAscii = 8) = False Then
        KeyAscii = 0
    End If
End Sub
  • 限制特殊符号,只能输入汉字和字母
Private Sub txtCoursename_KeyPress(KeyAscii As Integer)
    If KeyAscii < 0 Or KeyAscii = 8 Or KeyAscii = 13 Then
        ElseIf Not Chr(KeyAscii) Like "[a-zA-Z]" Then
        KeyAscii = 0
    End If
End Sub
  • 限制出生日期早于入学日期 
Dim borndate As Date  
Dim getdate As Date'定义变量  
borndate =Trim(txtBorndate.Text)  
getdate =Trim(txtRudate.Text)  
If getdate<=borndate then'进行比较  
    MsgBox"入学时间不能早于出生时间,请重新输入",vbOKOnly + vbInformation,"警告"  
    txtRudate.SetFocus  
    Exit Sub  
End If  
  • 限制文本框的输入长度
txtClassno.MaxLength = 10
  • 限制文本框输入内容的取值范围
If Val(txtClassno.Text) > 2147483647 Or Val(txtClassno.Text) < 1Then
    MsgBox "输入数值在1到2147483647范围内"
    txtClassno.SetFocus
    Exit Sub
End If
  • 限制家庭住址不能输入特殊字符。
Private Sub txtAddress_KeyPress(KeyAscii As Integer)
    Dim cTemp As String
    cTemp = "`~!@#$%^&*()-=_+[]{};:'\|<>/?.‘“”’、,。——+()《》?,~·……¥!:;【】" & """ '禁止输入特殊的字符"
    If InStr(1, cTemp, Chr(KeyAscii)) <> 0 Then
        KeyAscii = 0
    End If 
End Sub

 

  • 限制text框不可以输入

 locked属性锁定 。 

  • 显示combobox控件不可以输入

style属性值为2进行锁定。  

 

【后记】

暂时先总结这些吧,欢迎斧正~

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张_Laura

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值