VB代码片总结

前端之家收集整理的这篇文章主要介绍了VB代码片总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 特殊符号的限制

(1)需设定是哪几个特殊符号

Dim a As String,b As Long,c As Long
Dim i As Integer
a = "'!@#$%^&*()_+ "
For i = 1 To 20
    txtPassword.Text = Replace(txtPassword.Text,Mid(a,i,1),"")
Next

(2)ASCII码限制输入符号

事件为:KeyPress

- 限制特殊字符

If (KeyAscii >= 0 And KeyAscii <= 47) Or (KeyAscii >= 58 And KeyAscii <= 64) Or (KeyAscii >= 91 And KeyAscii <= 96) Or (KeyAscii >= 123 And KeyAscii <= 127) Then KeyAscii = 0

- 只允许输入数字

If KeyAscii = 8 Then Exit Sub     
    Select Case KeyAscii    
           Case 48 To 57       
           Case Else    
           KeyAscii = 0    
    End Select

- 只允许输入文本

If (KeyAscii < 0) Or (KeyAscii >= 65 And KeyAscii <= 90) Or(KeyAscii >= 97 And KeyAscii <= 122) Or (KeyAscii = 8) Then    
    Else    
          MsgBox "请输入字母或汉字",vbOKOnly,"提示"    
          KeyAscii = 0    
    End If

特殊符号的限制方法还有很多种

2. 窗体美观优化

使窗体不能随便更改大小:BorderStyle改为1-FixedSingle
窗体居中显示

With Me
          .BorderStyle = 0
          .Left = FrmMain.ScaleWidth / 2 - .Width / 2
          .Top = FrmMain.ScaleHeight / 2 - .Height / 2
  End With

3. 密码输入3次错误,设提示

miCount = miCount + 1
  If miCount > 3 Then
           MsgBox "你已经超过允许验证次数!",vbOKOnly + vbExclamation,"提示"
           Me.Hide
           Exit Sub
  End If

猜你在找的VB相关文章