Search code examples
vbafocususerform

Makeshift Cue Banners


I'm trying to create some cue banners for my user form in Word. I got about half way through before I become stuck. I have it where the cue banner will disappear and clear the textbox once it has focus. If the user types their own text, that will be retained as well.

However, if the user doesn't type anything in the textbox once it has been cleared, I want to replace the cue banner along with its attributes (gray text and italicized). I can't seem to get it to work. Here's the code with everything related to this textbox below. The trouble is with the Leave event I think.

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

Solution

  • I'm putting this here for anyone else who would like to know the solution. After a few days of messing with it, I finally realize I was over complicating things when I was messing with another piece of code. I eliminated the ActiveControl test because this is essentially already built into the Enter and Exit events. Here's the updated and working code.

    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)