多谢大家拉。。 不过有个方法似乎蛮复杂是用函数来写的 大家看看~ Public Function pfunGetLen(StrText As String) As Integer Dim i As Integer Dim StrLen As Integer StrLen = 0 For i = 1 To Len(StrText) If Asc(Mid(StrText, i, 1)) > 0 Then StrLen = StrLen + 1 Else StrLen = StrLen + 2 End If Next pfunGetLen = StrLen End FunctionPrivate Function funLengthControl(textBoxName As TextBox, KeyAscii As Integer) As Integer If pfunGetLen(Replace(textBoxName.Text, textBoxName.SelText, "") & Chr(KeyAscii)) > textBoxName.maxLength And KeyAscii <> 8 Then funLengthControl = 0 Else If KeyAscii = vbKeyReturn And pfunGetLen(Replace(textBoxName.Text, textBoxName.SelText, "")) > textBoxName.maxLength - 2 Then funLengthControl = 0 Else funLengthControl = KeyAscii End If End If End Function
msgbox ""
exit sub
end if
3?lenb(strconv("abc测试",vbFromUnicode))
7(一个汉字算两个英文字长)
3?lenb(strconv("abc测试",vbFromUnicode))
7(一个汉字算两个英文字长)
不过有个方法似乎蛮复杂是用函数来写的
大家看看~
Public Function pfunGetLen(StrText As String) As Integer
Dim i As Integer
Dim StrLen As Integer
StrLen = 0
For i = 1 To Len(StrText)
If Asc(Mid(StrText, i, 1)) > 0 Then
StrLen = StrLen + 1
Else
StrLen = StrLen + 2
End If
Next
pfunGetLen = StrLen
End FunctionPrivate Function funLengthControl(textBoxName As TextBox, KeyAscii As Integer) As Integer
If pfunGetLen(Replace(textBoxName.Text, textBoxName.SelText, "") & Chr(KeyAscii)) > textBoxName.maxLength And KeyAscii <> 8 Then
funLengthControl = 0
Else
If KeyAscii = vbKeyReturn And pfunGetLen(Replace(textBoxName.Text, textBoxName.SelText, "")) > textBoxName.maxLength - 2 Then
funLengthControl = 0
Else
funLengthControl = KeyAscii
End If
End If
End Function