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浜嬩欢镓嶅彂鐢熴