Excel VBA占位符文本,仅在键入时消失,而不是在UserFrom上输入textbox时消失

Excel VBA占位符文本,仅在键入时消失,而不是在UserFrom上输入textbox时消失,excel,vba,placeholder,Excel,Vba,Placeholder,'我希望创建占位符文本(重影文本)来帮助用户知道在字段中键入什么,但我希望它的行为与在线表单非常类似,在在线表单中,占位符文本不会在输入文本框时消失,而只有在您向其中键入新文本时才会消失 ' enterfieldbehavior is set to 1 - fmEnterFieldBehaviorRecallSelection in properties to avoid selecting placeholder text Private Sub userform_initialize()

'我希望创建占位符文本(重影文本)来帮助用户知道在字段中键入什么,但我希望它的行为与在线表单非常类似,在在线表单中,占位符文本不会在输入文本框时消失,而只有在您向其中键入新文本时才会消失

' 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
在属性中将enterfieldbehavior设置为1-fmEnterFieldBehaviorRecallSelection以避免选择占位符文本 私有子用户表单_初始化() TextBox2.Value=“Name”'启动UserForm时,占位符文本将在文本框中启动 TextBox2.ForeColor=&H800000C'灰色 端接头 专用子文本框2_Enter() 如果文本框2.文本“名称”,则 TextBox2.SelStart=TextBox2.SelLength'进入文本框后,光标仅位于占位符文本的开头,而不是中间或结尾 其他的 '我需要与上面相反的内容,当占位符文本消失时,将光标放在文本的末尾 如果结束 端接头 专用子文本框2u鼠标向下(ByVal按钮为整数,ByVal移位为整数,ByVal X为单个,ByVal Y为单个) TextBox2.SelStart=TextBox2.SelLength'如果用户使用鼠标输入文本框 端接头 专用子文本框2_Change() 如果文本框2.文本“名称”,则 TextBox2.Text=“” TextBox2.ForeColor=&H800000C'灰色 其他的 TextBox2.Value=TextBox2.Value'这是我丢失的地方,因为我想使用持有者文本,让用户键入他们想要的任何内容 TextBox2.ForeColor=vbBlack 如果结束 端接头 私有子文本框2_退出(ByVal Cancel作为MSForms.ReturnBoolean) 如果TextBox2.Text=”“,则 TextBox2.Text=“Name”'如果文本框没有更改,请替换占位符文本 TextBox2.ForeColor=&H800000C'灰色 其他的 如果结束 端接头
以下是我的做法:

Private Sub Label1_Click()
    TextBox1.SetFocus
End Sub

Private Sub TextBox1_Change()
   Label1.Visible = Len(TextBox1.Text) = 0
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Label1.Visible = Len(TextBox1.Text) = 0
End Sub

Private Sub UserForm_Initialize()
    With Label1
        .SpecialEffect = fmSpecialEffectSunken
        .BackColor = vbGrayText
        .Caption = "    Name"
    End With
End Sub

如果文本为空或等于“Name”,则将text=“Name”和color设置为灰色,则可能存在重复。否则,只需将颜色设置为黑色(文本将由控件本身设置)。经典思维。以及退出事件中语法的经典命令。