【问题标题】:Makeshift Cue Banners临时提示横幅
【发布时间】:2016-11-27 16:23:25
【问题描述】:

我正在尝试在 Word 中为我的用户表单创建一些提示横幅。在我被卡住之前,我已经完成了大约一半。我有它,提示横幅将消失并在获得焦点后清除文本框。如果用户键入自己的文本,该文本也会被保留。

但是,如果用户在清除文本框后没有在文本框中输入任何内容,我想替换提示横幅及其属性(灰色文本和斜体)。我似乎无法让它工作。下面是与此文本框相关的所有内容的代码。我认为问题出在 Leave 事件上。

Private Sub UserForm_Initialize()

        Me.txbShipToName1.Text = "name"
        Me.txbShipToName1.Font.Italic = True
        Me.txbShipToName1.ForeColor = &H80000006

End Sub

Private Sub txbShipToName1_Enter()

        If Me.ActiveControl Is Me.txbShipToName1 And Me.txbShipToName1.Text = "name" Then
            txbShipToName1.Font.Italic = False
            txbShipToName1.ForeColor = &H80000008
            txbShipToName1.Text = ""
        End If

End Sub

Private Sub txbShipToName1_Leave()

        If Me.ActiveControl Is Not txbShipToName1 And Me.txbShipToName1.Text = "" Then
            txbShipToName1.Font.Italic = True
            txbShipToName1.ForeColor = &H80000006
            txbShipToName1.Text = LCase(txbShipToName1.Text)
            txbShipToName1.Text = "name"
        End If

End Sub

Private Sub txbShipToName1_Change()

        If Me.ActiveControl Is Me.txbShipToName1 And Me.txbShipToName1 <> "name" Then
            txbShipToName1.Text = UCase(txbShipToName1.Text)
       End If

End Sub

【问题讨论】:

    标签: vba focus userform


    【解决方案1】:

    我把这个放在这里给任何想知道解决方案的人。在搞砸了几天之后,我终于意识到,当我在搞乱另一段代码时,我把事情复杂化了。我取消了 ActiveControl 测试,因为它基本上已经内置在 Enter 和 Exit 事件中。这是更新后的工作代码。

    Private Sub UserForm_Initialize()
    'Populates cue banners
        Me.txbShipToName1.Text = "Name"
        Me.txbShipToName1.Font.Italic = True
        Me.txbShipToName1.ForeColor = &H80000011
    End Sub
    
    Private Sub txbShipToName1_Enter()
    'Removes cue banner upon entering textbox
        If Me.txbShipToName1.Text = "Name" Then
            txbShipToName1.Font.Italic = False
            txbShipToName1.ForeColor = &H80000012
            txbShipToName1.Text = ""
        End If
    
    End Sub 'txbShipToName1_Enter()
    Private Sub txbShipToName1_Change()
    
    'Converts textbox to uppercase
        If Me.txbShipToName1.Text <> "Name" Then
            txbShipToName1.Text = UCase(txbShipToName1.Text)
        End If
    End Sub 'txbShipToName1_Change()
    
    Private Sub txbShipToName1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'Replaces cue banner upon exiting textbox
        If Me.txbShipToName1.Text = "" Then
            txbShipToName1.Font.Italic = True
            txbShipToName1.ForeColor = &H80000011
            txbShipToName1.Text = "Name"
        End If
    End Sub 'txbShipToName1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 2016-03-29
      相关资源
      最近更新 更多