VB 6 文本框中的大写

Upper Case in VB 6 text box

如何在 vb 6.0 中按 Tab 或 space 时将第一个字母变成大写?

我的代码如下

txtFirstName.Text = UCase$(txtFirstName.Text)

但在 tab 或 space

后不会改变
StrConv Function


Returns a Variant (String) converted as specified. 

Syntax

StrConv(string, conversion, LCID)

The StrConv function syntax has these named arguments:

Part Description 
string Required. String expression to be converted. 
conversion Required. Integer. The sum of values specifying the type of conversion to perform. 
LCID Optional. The LocaleID, if different than the system LocaleID. (The system LocaleID is the default.) 


Settings

The conversion argument settings are:

Constant Value Description 
vbUpperCase 1 Converts the string to uppercase characters. 
vbLowerCase 2 Converts the string to lowercase characters. 
vbProperCase 3 Converts the first letter of every word in string to uppercase. 

AND THERE IS MORE ...

给 GSERGE

$ 应用于函数名而不是变量名时没有任何意义。 VBA 使用 $ AND B 作为后缀来表示相似的功能。

VB6 是 VBA 可能在 VB6 而不是 VBA 的人。 VB6 程序主机 VBA 作为他们的编程语言。 VB6 本身只是一些应用程序对象和表单包——没有编程语言。最好将 VB6 视为像 Office 这样的 VBA 主机。

如果您想正确区分大小写,请参阅此 WORDBASIC Ver 6 代码,(其中 2003 字有助于转换为 vba)。

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Sub MAIN()
Select Case WordBasic.Int(GetModifer)
        Case 0
                WordBasic.ChangeCase
        Case 1
                WordBasic.ChangeCase 4
        Case 2
                WordBasic.ChangeCase 2
        Case 3
                ProperCase
        Case Else
                WordBasic.ChangeCase
End Select
End Sub

Private Sub ProperCase()
Dim F
Dim z
Dim a$
Dim P
F = 1
WordBasic.ChangeCase 2
WordBasic.EditBookmark Name:="SerenityChangeCase", SortBy:=0, Add:=1
z = WordBasic.GetSelEndPos()
WordBasic.CharLeft 1
        While WordBasic.GetSelEndPos() < z And Not WordBasic.AtEndOfDocument()
                WordBasic.SelectCurWord
                a$ = WordBasic.[Selection$]()
                P = 0
                If LCase(a$) = "a" Then
                        P = 1
                ElseIf LCase(a$) = "an" Then
                        P = 1
                ElseIf LCase(a$) = "as" Then
                        P = 1
                ElseIf LCase(a$) = "at" Then
                        P = 1
                ElseIf LCase(a$) = "be" Then
                        P = 1
                ElseIf LCase(a$) = "by" Then
                        P = 1
                ElseIf LCase(a$) = "in" Then
                        P = 1
                ElseIf LCase(a$) = "is" Then
                        P = 1
                ElseIf LCase(a$) = "of" Then
                        P = 1
                ElseIf LCase(a$) = "on" Then
                        P = 1
                ElseIf LCase(a$) = "or" Then
                        P = 1
                ElseIf LCase(a$) = "to" Then
                        P = 1
                ElseIf LCase(a$) = "and" Then
                        P = 1
                ElseIf LCase(a$) = "are" Then
                        P = 1
                ElseIf LCase(a$) = "for" Then
                        P = 1
                ElseIf LCase(a$) = "the" Then
                        P = 1
                ElseIf LCase(a$) = "from" Then
                        P = 1
                ElseIf LCase(a$) = "what" Then
                        P = 1
                ElseIf LCase(a$) = "with" Then
                        P = 1
                End If
                If P = 1 And F = 0 Then WordBasic.Insert LCase(a$)
                WordBasic.WordRight 1
                F = 0
        Wend
WordBasic.WW7_EditGoTo Destination:="SerenityChangeCase"
WordBasic.EditBookmark Name:="SerenityChangeCase", SortBy:=0, Delete:=1
End Sub

Private Function GetModifer()
Dim a
Dim B
Dim c
Dim X
        a = GetAsyncKeyState(16)
        B = GetAsyncKeyState(17)
        c = GetAsyncKeyState(18)
        X = 0
        If a < 0 Then X = X + 1
        If B < 0 Then X = X + 2
        If c < 0 Then X = X + 4
GetModifer = X
End Function

使用LostFocus事件

Private Sub yourTextBox_LostFocus()
    With yourTextBox
        'first letter in upper case, the rest, untouched.
        .Text = UCase(Mid(.Text, 1, 1)) & Mid(.Text, 2, Len(.Text))
    End With
End Sub

将相同的逻辑应用于KeyDown事件并检查按下的键是否为space键。

Private Sub yourTextBox_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 32 Then
        With yourTextBox
            'first letter in upper case, the rest, untouched.
            .Text = UCase(Mid(.Text, 1, 1)) & Mid(.Text, 2, Len(.Text))
            .SelStart = Len(.Text) 'put the cursor at the end of the textbox...
        End With
    End If
End Sub

这很简单,只需在文本框按键事件中执行此操作...

Private sub textbox_keypress(KeyAscii As Integer)
      KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

好的。是的,txtFirstName 是一个很好的用法指标。所以我会使用(某种)标题大写,我会在验证事件中这样做。所以

Private Sub txtFirstName_Validate(Cancel As Boolean)
    Dim p As Integer ' i doubt we'll use more than 32K for a name....
    Dim mName As String
    p = 1
    ' first off lets trim any leading blanks.. assume NOTHING and make sure its all lower case..
    mName = LCase(LTrim(txtFirstName))
    Do While p > 0 And p <= Len(txtFirstName) ' start with the first non-blank
        
        Mid(mName, p, 1) = UCase(Mid(mName, p, 1))
        p = InStr(p, mName, " ")
        If p > 0 And p < Len(mName) Then p = p + 1
    Loop
    Cancel = False
    txtFirstName = mName
End Sub

每次都有效,并且每个单词都大写。没有添加任何代码来实现真正的标题上限,但这很接近,而且简短且容易...