vba Excel VBA中的文本框提示文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8514760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:42:31  来源:igfitidea点击:

Text box prompt text in Excel VBA

excelvbatextbox

提问by Wes

You know how sometimes you see text in an input field prompting for an answer? When you click it, it goes away, but comes back if you deselect without having typed anything.

您知道有时您会在输入字段中看到提示答案的文本吗?当您单击它时,它会消失,但如果您在未输入任何内容的情况下取消选择,它就会返回。

How do I accomplish this in an Excel text box? Can this be done at all?

如何在 Excel 文本框中完成此操作?这完全可以做到吗?

I think I could accomplish this if I attach a click macro to the text box and the macro can be able to tell what text box was clicked, but the problem with the click macro is it won't let you select the object first, so you have no idea what was clicked. And I don't want to hard code the text box name in the click macro code.

我想如果我将 Click 宏附加到文本框,并且宏可以判断单击了哪个文本框,我想我可以完成此操作,但是 Click 宏的问题是它不会让您先选择对象,所以你不知道点击了什么。而且我不想在 Click 宏代码中对文本框名称进行硬编码。

回答by Michael

I think you're referring to a "placeholder" in a UserForm. The two tricks that I use to achieve this are the "tag" property and Enterand Exitevents. First, create your sub modules that will fire upon Enter(focus) and Exit(blur).

我认为您指的是用户窗体中的“占位符”。我用来实现这一点的两个技巧是“标签”属性和EnterExit事件。首先,创建将在Enter(聚焦)和Exit(模糊)时触发的子模块。

Sub TB_enter(TB_name)
    If Len(Me.Controls(TB_name).Tag) = 0 Then
        Me.Controls(TB_name).Tag = Me.Controls(TB_name).Value
        Me.Controls(TB_name).Value = vbNullString
    End If
End Sub
Sub TB_exit(TB_name)
    If Len(Me.Controls(TB_name).Value) = 0 Then
        Me.Controls(TB_name).Value = Me.Controls(TB_name).Tag
        Me.Controls(TB_name).Tag = vbNullString
    End If
End Sub

Next, for each instance of your textbox on your userform, double click on the textbox while in View Object mode. This will automatically create a Private Sub for the change event. Head to the upper right hand corner of the toolbar and change the "Change" drop down list to "Enter". This will create a new Private Sub for the Enterevent. Repeat this process again, but select "Exit" to create the Exitevent. Now, simply copy the following code into the Enterand Exitevents respectively (I've used the default name for the first textbox, but this is name independent when calling the events).

接下来,对于用户表单上文本框的每个实例,在查看对象模式下双击文本框。这将自动为更改事件创建一个 Private Sub。前往工具栏的右上角并将“更改”下拉列表更改为“输入”。这将为Enter事件创建一个新的 Private Sub 。再次重复此过程,但选择“退出”以创建退出事件。现在,只需将以下代码分别复制到EnterExit事件中(我使用了第一个文本框的默认名称,但在调用事件时这是名称独立的)。

Private Sub TextBox1_Enter()
    TB_enter ActiveControl.Name
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TB_exit ActiveControl.Name
End Sub

If you are placing these calls in an external module, simply replace "Me" with the name of the UserForm.

如果您将这些调用放置在外部模块中,只需将“我”替换为用户窗体的名称。

回答by Jesse

You could do it with a couple of events. To clear the box on click is pretty easy:

你可以通过几个事件来做到这一点。单击时清除框非常简单:

Private Sub TextBox1_GotFocus()
    TextBox1.Text = ""
End Sub

I'd add some other steps, add a worksheet_activate event to set an inital value and set a flag for if the default value is set:

我会添加一些其他步骤,添加一个 worksheet_activate 事件来设置初始值并为是否设置默认值设置一个标志:

Private Sub Worksheet_Activate()
    Dim textBox1Default As Boolean
    TextBox1.Text = "Please enter some text here"
    textBox1Default = True
End Sub

Then I'd add a TextBox1_Change event to remove the default flag

然后我会添加一个 TextBox1_Change 事件来删除默认标志

Private Sub TextBox1_Change()
    If textBox1Default = True Then textBox1Default = False
End Sub

And finally adjust the original GotFocus event to include the flag check:

最后调整原始 GotFocus 事件以包含标志检查:

Private Sub TextBox1_GotFocus()
    If textBox1Default Then TextBox1.Text = ""
End Sub

You'll need to make it work for you, this way if you change tabs it will reset the text but you can have it check that on activate and change it to suit when you want it to reset.

你需要让它为你工作,这样如果你改变标签,它会重置文本,但你可以让它在激活时检查,并在你想要重置时更改它以适应。

Jesse

杰西