新建From1(窗体),新建Text1(TextBox),代码:
Dim KP As Boolean
Dim SB As String
Dim strResult As String
Private Sub Text1_KeyPress(KeyAscii As Integer)
On Error Resume Next
If Len(Text1) = 1 Then
strResult = Text1
' Set first letter on left of string to uppercase
strResult = UCase$(Left$(strResult, 1)) & Mid$(strResult, 2)
' Place results of uppercase statement in txtField
Text1 = strResult
' Place cursor at end of txt string
Text1.SelStart = Len(strResult) + 1
' Check to see if Spacebar was pressed, Y=Yes
ElseIf SB = "Y" Then
' strTemp is the 1st letter after the spacebar is pressed & set to uppercase
strTemp = UCase$(Right$(Text1, 1))
' txtField becomes strresult + the uppercase strTemp
Text1 = strResult & strTemp
' Place the cursor at the end of the string
Text1.SelStart = Len(strResult) + 1
' Set SB to NULL
SB = ""
End If
strResult = Text1.Text
If KeyAscii = 32 Then
' If the spacebar is pressed then KP is true
KP = True
' Spacebar was pressed on previous key
ElseIf KP = True Then
' Set SB to Yes identifying spacebar was pressed
SB = "Y"
' Set KP to False until next time spacebar is pressed
KP = False
End If
End Sub
比如输入ma nong ku就变成了Ma Nong Ku,好玩吧?