Makeshift Cue Banners

时间:2016-11-27 16:23:26

标签: vba focus userform cue

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

但是,如果用户在清除后没有在文本框中键入任何内容,我想替换cue标题及其属性(灰色文本和斜体)。我似乎无法让它发挥作用。以下是与此文本框相关的所有代码。麻烦在于我认为离开事件。

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

1 个答案:

答案 0 :(得分:0)

我想把这个放在其他想知道解决方案的人身上。经过几天的捣乱,我终于意识到当我弄乱另一段代码时,我已经结束了复杂化。我取消了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)