【问题标题】:Upper Case in VB 6 text boxVB 6 文本框中的大写
【发布时间】:2015-04-08 12:02:50
【问题描述】:

如何在vb 6.0中按制表符或空格时将第一个字母变为大写?

我的代码如下

txtFirstName.Text = UCase$(txtFirstName.Text)

但在制表符或空格后不会改变

【问题讨论】:

  • $ 符号有什么用?
  • @Nadeem_MK 这是老式的 VB6 语法。 VB6 没有本地方法来仅更改第一个字母。我们也不知道代码的位置或如何(甚至是否)调用它。
  • @Plutonix 这与老式语法无关。 VB6 中的字符串函数成对存在——一个接受并返回Variants,另一个接受并返回Strings。强类型的以$ 结尾。两者在不同的上下文中都很有用,所以如果你有一个字符串变量开头,最好使用UCase$而不是UCase
  • @GSerg、RTrim$UCase$ 等实际上是非常可追溯到 ROM Basic 的老派。非类型说明符变体(UCase)是相对较新的(VB5 或 6)。但是两个版本都接受并返回字符串。也许 vba 或其他东西使用变体,但不是 VB6。从对象浏览器(具有两个定义):Function UCase(String) ...Returns the specified string, converted to uppercase
  • @Plutonix String 这里是参数的名称,而不是类型。当类型为Variant 时,对象浏览器会完全忽略它。与UCase$(String As String) 比较。还要注意UCase(String) 没有返回类型,这再次意味着返回类型被省略,因为它是Variant。另一个是UCase$(String As String) As String。同样的事情发生在 VBA 中。但是你说得对,美元符号本身起源于很久以前。

标签: vba vb6 vb6-migration


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    使用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事件应用相同的逻辑,并检查按下的键是否为空格键。

    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
    

    【讨论】:

      【解决方案3】:

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

      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
      

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

      【讨论】:

        【解决方案4】:
        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 IS 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
        

        【讨论】:

        • vbProperCase 将更改所有首字母,而不是 首字母。
        • 文本中只有一个单词。阅读整个问题,这是某人的名字。
        • 我阅读了整个问题,但没有找到“文本只有一个单词”。同时,我知道多字姓氏,并非所有单词都必须大写(例如“der”)。
        • txtFirstName 是线索
        • @GSerg 非常沉默,不是你
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-29
        相关资源
        最近更新 更多