vb里字符串的长度是1字符和1汉字都是1个长度,要想限定长度,请参看下列代码
自己写吧,记得给分
'限制text1的maxlength(即一个字符占一个,一个汉字占两个
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim tmpArr() As Byte
Dim MyStr As String
MyStr = Trim(Text1.Text)
For i = 0 To Text1.MaxLength - Len(MyStr)
MyStr = MyStr & " "
Next i
tmpArr = StrConv(MyStr, vbFromUnicode)
If Text1.MaxLength = 0 Then Exit Sub
If tmpArr(Text1.MaxLength - 1) <> 32 And KeyAscii <> 8 Then
KeyAscii = 0
Exit Sub
End If
End Sub
自己写吧,记得给分
'限制text1的maxlength(即一个字符占一个,一个汉字占两个
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim tmpArr() As Byte
Dim MyStr As String
MyStr = Trim(Text1.Text)
For i = 0 To Text1.MaxLength - Len(MyStr)
MyStr = MyStr & " "
Next i
tmpArr = StrConv(MyStr, vbFromUnicode)
If Text1.MaxLength = 0 Then Exit Sub
If tmpArr(Text1.MaxLength - 1) <> 32 And KeyAscii <> 8 Then
KeyAscii = 0
Exit Sub
End If
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货