查看: 277  |  回复: 0
VB6 让每个单词的第一个字母变成大写,单词可以用空格隔开
楼主
发表于 2023年5月7日 16:46

新建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,好玩吧?

您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2024 MaNongKu.com