Announcement

Collapse
No announcement yet.

Placeholder text that disappears only upon typing, not when entering textbox

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Placeholder text that disappears only upon typing, not when entering textbox



    I'm looking to create placeholder text (ghosting text) to help users know what to type in the field, but I want it to act very similar to on-line forms where the placeholder text does not disappear upon entering a textbox, but only disappears if you type new text into it.
    Code:
    ' enterfieldbehavior is set to 1 - fmEnterFieldBehaviorRecallSelection in properties to avoid selecting placeholder text
    
    Private Sub userform_initialize()
    TextBox2.Value = "Name" 'upon starting UserForm, the placeholder text is launched in the textbox
    TextBox2.ForeColor = &H8000000C 'grey
    
    End Sub
    
    Private Sub TextBox2_Enter()
    If TextBox2.Text <> "Name" Then
    TextBox2.SelStart = TextBox2.SelLength 'Upon entering the textbox, the cursor is placed only at the start and not the middle or end of the placeholder text
    Else
    ' I need the oppositie of the above, to put the cursor at the end of text as the placeholder text is gone
    End If
    
    End Sub
    
    Private Sub TextBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    TextBox2.SelStart = TextBox2.SelLength ' If a user uses the mouse to enter the textbox
    
    End Sub
    
    Private Sub TextBox2_Change()
    If TextBox2.Text <> "Name" Then
    TextBox2.Text = ""
    TextBox2.ForeColor = &H8000000C 'grey
    Else
    TextBox2.Value = TextBox2.Value ' This is where I'm lost as I want to earse the holder text, and let the user type whatever they want
    TextBox2.ForeColor = vbBlack
    End If
    
    End Sub
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox2.Text = "" Then
    TextBox2.Text = "Name" ' If there are no changes to the textbox, replace the placeholder text
    TextBox2.ForeColor = &H8000000C 'grey
    Else
    End If
    
    End Sub

  • #2
    This any use?
    Attached Files

    Comment


    • #3
      Wow! Trunten, thank you so much for your help! It works brilliantly!

      Comment


      • #4
        Followup question - Most people would use ENTER or TAB keys to navigate from one textbox to the next; however, if a user were to click to enter a textbox with their mouse in the middle of the placeholder text, and begin to type, it would "break it" and make the placeholder text become part of what's being currently typed. Is there a solution to this? Thank you again for your help!

        Comment


        • #5


          I'd just do something like this to basically disallow mouse selection whilst the place holder text exists.
          Attached Files

          Comment

          Working...
          X