1. vb涓鏂囨湰妗嗙殑validate浜嬩歡鎬庝箞鐢錛
Validate浜嬩歡
鍦ㄧ劍鐐硅漿鎹㈠埌涓涓錛堢浜屼釜錛夋帶浠朵箣鍓嶅彂鐢燂紝姝ゆ椂璇ユ帶浠剁殑 CausesValidation 灞炴у艱劇疆涓篢rue銆
璇娉
Private Sub object_Validate(KeepFocus As Boolean)
Validate浜嬩歡銆 CausesValidation灞炴хず渚
璇ョず渚嬩嬌鐢ㄤ笁涓鎺т歡鏉ョず鑼僔alidate 浜嬩歡鍜孋ausesValidation 灞炴х殑浣跨敤銆傚湪緙虹渷鎯呭喌涓嬶紝涓や釜TextBox 鎺т歡鐨凜ausesValidatio n灞炴ц劇疆涓 True錛岃繖鏍峰綋鎮ㄦ兂鎶婄劍鐐逛粠涓涓猅extBox杞鎹㈠埌鍙︿竴涓鏃訛紝Validate 浜嬩歡鍙戠敓銆傚傛灉 Text1娌℃湁鍖呭惈鏃ユ湡鎴 Text2娌℃湁鍖呭惈涓涓澶т簬10鐨勬暟瀛楋紝鐒︾偣鐨勮漿鎹㈠皢琚闃繪銆傜敱浜 Command1鎺т歡鐨凜ausesValidation 灞炴ц劇疆涓 False錛屽洜姝ゆ偍鏃犺轟綍鏃墮兘鍙浠ュ崟鍑籋elp 鎸夐挳銆
瑕佽瘯楠岃ョず渚嬶紝鍦ㄧ獥浣撲腑鏀劇疆涓涓狢ommandButton 鍜屼袱涓猅extBox 鎺т歡錛屽皢浠g爜綺樻帴鍒扮獥浣撶殑鈥滃0鏄庘濋儴鍒嗗苟榪愯屾ゅ伐紼嬨傛寜Tab 閿灝濊瘯杞鎹㈢劍鐐廣
Private Sub Form_Load()
'璁劇疆鎸夐挳鐨凜ausesValidation灞炴т負False銆傚綋鐢ㄦ埛
'鍗曞嚮鎸夐挳鏃訛紝Validate浜嬩歡涓嶅彂鐢熴
'璁劇疆鎸夐挳鐨凜aption灞炴т負鈥滃府鍔┾濄
With Command1
.CausesValidation = False
.Caption = "Help"
End With
Show
With Text1 '閫夋嫨Text1鐨勬枃鏈騫朵負瀹冭劇疆鐒︾偣銆
.SelLength = Len(Text1.Text)
.SetFocus
End With
End Sub
Private Sub Command1_Click()
'褰撳崟鍑繪ゆ寜閽鏃剁粰鍑虹敤鎴峰府鍔╀俊鎮銆
MsgBox _
"Text1 must be set to a date." & VbCrLF & _
"Text2 must be a number less than 10."
End Sub
Private Sub Text1_Validate(KeepFocus As Boolean)
'濡傛灉鍊間笉鏄涓涓鏃ユ湡錛屽垯淇濇寔鐒︾偣錛岄櫎闈炵敤鎴
'鍗曞嚮Help銆
If Not IsDate(Text1.Text) Then
KeepFocus = True
MsgBox "Please insert a date in this field.", , "Text1"
End if
End Sub
Private Sub Text2_Validate(KeepFocus As Boolean)
'濡傛灉鍊兼槸涓涓澶т簬10鐨勬暟瀛楋紝淇濇寔鐒︾偣銆
If Not IsNumeric(Text2.Text) Or Val(Text2.Text) > 10 Then
KeepFocus = True
MsgBox _
"Please insert a number less than or equal to 10.", , "Text2"
End If
End Sub
Validate浜嬩歡璇娉曞寘鍚涓嬮潰鍑犻儴鍒嗭細
閮ㄥ垎 鎻忚堪
object 涓涓瀵硅薄琛ㄨ揪寮忥紝鍏跺間負鈥滃簲鐢ㄤ簬鈥濆垪琛ㄤ腑鐨勪竴涓瀵硅薄銆
KeepFocus 紜瀹氭帶浠舵槸鍚﹀け鍘葷劍鐐圭殑鍊箋侹eepFocus璁劇疆涓篢rue鏃訛紝鎺т歡淇濇寔鐒︾偣銆
璇存槑
Validate 浜嬩歡鍜孋ausesValidation 灞炴у崗鍚屽伐鎬嶏紝闃叉㈡帶浠跺け鍘葷劍鐐圭洿鍒版弧瓚崇『瀹氱殑鍑嗗垯銆
閲嶇偣 鍙鏈夊湪鍗沖皢鑾峰緱鐒︾偣鐨勬帶浠剁殑CausesValidation灞炴у艱劇疆涓篢rue鏃訛紝Validate浜嬩歡鎵嶅彂鐢熴