如何在Text中只能输入一个汉字?

来源:百度知道 编辑:UC知道 时间:2024/05/30 05:41:41
Text1.MaxLength = 2
这样的代码设置以后是只能输入2个字母了,但不能限制只能输入一个汉字!
怎么写才可以只能写一个汉字或2个字母在里面!输入多了只取输入的汉字的第1个字?

Private Sub Text1_Change()
Static mTestTemp As String
Dim t1 As TextBox
Dim i As Integer
Set t1 = Text1
If LenB(StrConv(t1.Text, vbFromUnicode)) > t1.MaxLength Then
i = t1.SelStart
t1.Text = mTestTemp
If i > 0 Then t1.SelStart = i - 1
Else
mTestTemp = t1.Text
End If

End Sub

Text1的Change事件里面添上如上代码就可以了