學生總結——優化篇
阿新 • • 發佈:2018-11-11
一:限制輸入
只能輸入數字:
If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 _
Or KeyAscii = 13 Then
Else
If KeyAscii = 32 Then
MsgBox "不能輸入空格!", vbOKOnly + vbExclamation, "警告"
KeyAscii = 0
Else
MsgBox "對不起,班號為數字!", vbOKOnly + vbExclamation, "警告"
KeyAscii = 0
txtClassno.Text = ""
txtClassno.SetFocus
只能輸入文字:
If KeyAscii < 0 Or KeyAscii = 8 Or KeyAscii = 13 Then
Else
If KeyAscii = 32 Then
MsgBox "不能輸入空格!", vbOKOnly + vbExclamation, "警告"
KeyAscii = 0
Else
MsgBox "對不起,姓名為漢字!", vbOKOnly + vbExclamation, "警告"
KeyAscii = 0
txtName.Text = ""
txtName.SetFocus
限制下拉框輸入字元:
KeyAscii = 0
combobox可以使用屬性限制(style選擇2即可)。
二:日期無需手動輸入
使用DTPicker控制元件
新增相應的程式碼。
入學日期不能超出當前日期。
If getdate > Date Then
MsgBox "入學日期不能早於當前日期" , vbOKOnly + vbExclamation, "警告"
入學日期不小於出生日期
If getdate < borndate Then
MsgBox "入學日期不能小於出生日期", vbOKOnly + vbExclamation, "警告"
出生日期不大於當前日期
If borndate > Date Then
MsgBox "出生時間不能大於當前時間", vbOKOnly + vbExclamation, "警告"
三:限制電話為11位
If Len(txtTel) < 11 Then
MsgBox "聯絡電話為11位,請檢查", vbOKOnly + vbExclamation, "警告"
四:不能重複新增課程
For selectcourse = 0 To listAllcourse.ListCount - 1
If listAllcourse.Selected(selectcourse) = True Then
listSelectcourse.AddItem listAllcourse.List(listAllcourse.ListIndex)
' 向listselectcourse列表中新增課程
For i = 0 To listSelectcourse.ListCount - 1
For j = i + 1 To listSelectcourse.ListCount
'判斷在list列表中是否有相同的名稱
If listSelectcourse.List(i) = listSelectcourse.List(j) Then
listSelectcourse.RemoveItem (j)
MsgBox "已新增此課程!", vbOKOnly + vbExclamation, "提示"