VB代码片总结

前言:

  前行的过程中,想要跑的快、跑的远,总结必不可少;

代码片:

1、特殊符号限制:

  • 限制特殊字符:
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 
End If
  • 只允许输入文本:
If (KeyAscii < 0) Or (KeyAscii >= 65 And KeyAscii <= 90) Or(KeyAscii >= 97 And KeyAscii <= 122) Or (KeyAscii = 8) Then    
     MsgBox "请输入字母或汉字", vbOKOnly,"提示"    
     KeyAscii = 0    
End If  
  • 只允许输入数字:
'只能输入数字
Private Sub Password_KeyPress(KeyAscii As Integer)
    If KeyAscii = 8 Then '8的ASCII码是退格键
        Exit Sub
    End If  

    '0的ASCII码是48,9的ASCII码是57
    If KeyAscii < 48 Or KeyAscii > 57 Then
        KeyAscii = 0
    End If
End Sub

2、点击X号关闭系统

    a = MsgBox("确定要退出吗?", vbOKCancel, "提示!")
    If a = vbOK Then
        End
    Else
        Cancel = True
        FrmMain.Show
    End If

3、Textbox与DTPicker交互:

If Combo1(0).Text = "上机日期" Or Combo1(0).Text = "下机日期" Then
        DTPicker1.Format = dtpCustom
        DTPicker1.CustomFormat = Format("yyyy-MM-dd")
        DTPicker1.Visible = True
        Text1.Visible = False
        Text1.Text = DTPicker1.Value
    Else
        If Combo1(0).Text = "上机时间" Or Combo1(0).Text = "下机时间" Then
            DTPicker1.Format = dtpTime
            DTPicker1.Visible = True
            Text1.Visible = False
            Text1.Text = DTPicker1.Value
        Else
            DTPicker1.Visible = False
            Text1.Visible = True
        End If
   End If

4、快速清空文本框、Combox的内容:

Dim ctl As Control   '定义一个变量,把控件装载到里面  
   For Each ctl In Controls 

    '删除所有text文本框内容  
       If TypeOf ctl Is TextBox Then 
           ctl.Text = ""  
   Next ctl  

   '删除所有combobox文本框内容  
   For Each ctl In Controls  
       If TypeOf ctl Is ComboBox Then 
           ctl.Text = ""  
   Next ct1

小结:

  记是永远记不住的,在不断重复学习的过程中,一遍遍的深入学习增加新的理解,寻找联系并不断总结;

猜你喜欢

转载自blog.csdn.net/qiqibei666/article/details/79439628