文本框Excel VBA上的文本框文本消失

时间:2014-02-03 19:39:17

标签: excel vba excel-vba

基本上我有一个我创建的Userform,我想知道是否可以在Userform加载时添加Gray文本,但是一旦用户开始在TextBox中输入文本就会消失:

Userform http://im34.gulfup.com/rDwF8.png

一旦用户开始输入字体颜色,应该变为黑色。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:8)

这样的东西?

Private Sub UserForm_Initialize()
    TextBox1.ForeColor = &HC0C0C0 '<~~ Grey Color
    TextBox1.Text = "Please Enter Name Here"
    CommandButton1.SetFocus '<~~ This is required so that the focus moves from TB
End Sub

Private Sub TextBox1_Enter()
    With TextBox1
        If .Text = "Please Enter Name Here" Then
            .ForeColor = &H80000008 '<~~ Black Color
            .Text = ""
        End If
    End With
End Sub

Private Sub TextBox1_AfterUpdate()
    With TextBox1
        If .Text = "" Then
            .ForeColor = &HC0C0C0
            .Text = "Please Enter Name Here"
        End If
    End With
End Sub

ScreenShot(在行动中)

enter image description here