原始代码:
重构代码-随笔(1)    If txtSchool.Text = "" Then
重构代码-随笔(1)        
MsgBox "请输入注册单位!", vbInformation
重构代码-随笔(1)        CheckRegText 
= False
重构代码-随笔(1)        txtSchool.SetFocus
重构代码-随笔(1)        
Exit Function
重构代码-随笔(1)    
End If

1:len(txtSchool.Text)  要比 txtSchool.Text=""快的多。

2:为txtSchool.Text加上Trim

3:去除txtSchool的重复。

结果:

1:StringIsEmpty函数
重构代码-随笔(1)'判断字符串的长度是否为空
重构代码-随笔(1)
Public Function StringIsEmpty(ByRef sString As StringAs Boolean
重构代码-随笔(1)    sString 
= Trim(sString)
重构代码-随笔(1)    StringIsEmpty 
= Len(sString) = 0
重构代码-随笔(1)
End Function

2:TextBoxIsEmpty函数
重构代码-随笔(1)'文本框为空
重构代码-随笔(1)
Private Function TextBoxIsEmpty(oTextBox As TextBox, sMessage As String) AS Boolean
重构代码-随笔(1)    
If StringIsEmpty(oTextBox.Text) Then
重构代码-随笔(1)        
MsgBox sMessage, vbInformation
重构代码-随笔(1)        oTextBox.SetFocus
重构代码-随笔(1)        TextBoxIsEmpty 
= True
重构代码-随笔(1)    
End If
重构代码-随笔(1)
End Function

3:最终效果
重构代码-随笔(1)    If TextBoxIsEmpty(txtPerson, "请输入注册人!"Then Exit Function
重构代码-随笔(1)    
重构代码-随笔(1)    
If TextBoxIsEmpty(txtConn, "请输入联系电话!"Then Exit Function
重构代码-随笔(1)    
重构代码-随笔(1)    
If TextBoxIsEmpty(txtSchool, "请输入注册单位!"Then Exit Function

相关文章: