在 VBA 中单击时清除文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11404965/
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-08 13:29:08  来源:igfitidea点击:

Clearing textbox upon click in VBA

excelvbaexcel-vbatextboxclear

提问by nobillygreen

I'm relatively new to VBA, and am working on a small database for my employer. I'm trying to create a text box that, when clicked, deletes the contents that I have put in it. For example, I have a text box that says "first" for them to put in the first name of a member. When they click it, I want my text to disappear so they can enter what they want. (The matter of it only executing once is easily solved with an if statement). I've taken a look at the following question, but neither answer worked for me;

我对 VBA 比较陌生,正在为我的雇主开发一个小型数据库。我正在尝试创建一个文本框,单击该文本框时会删除我放入其中的内容。例如,我有一个文本框,上面写着“第一个”,让他们输入成员的名字。当他们单击它时,我希望我的文本消失,以便他们可以输入他们想要的内容。(它只执行一次的问题很容易用 if 语句解决)。我查看了以下问题,但没有一个答案对我有用;

Clear text in textbox upon clicking it

单击文本框时清除文本框中的文本

The following code did nothing

以下代码什么也没做

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

And the following code give me an "Expected, end of statement" compilation error on 'Handles';

以下代码在“句柄”上给我一个“预期,语句结束”编译错误;

Private Sub txtNewNameHere_GotFocus() Handles txtNewNameHere.GotFocus
txtNewNameHere.Text = ""
End Sub

Not sure why either of those did not work for me, but, as I said, I'm relatively new to this. Any help greatly appreciated.

不知道为什么其中任何一个对我都不起作用,但是,正如我所说,我对此比较陌生。非常感谢任何帮助。

回答by danielpiestrak

Assuming that the name for your textbox is 'Text1' The following event will do what you're looking for:

假设您的文本框的名称是 'Text1' 以下事件将执行您要查找的操作:

Private Sub Text1_GotFocus()
    If Me.Text1.Value = "First" Then
        Me.Text1.Value = ""
    End If
End Sub

I always put the me. in front of the object to reference the form.

我总是把我。在对象前面引用窗体。

I was unclear if the name of your text box was 'TextBox1' or 'txtNewNameHere' from your above code examples so I just used a generic 'Text1' in the above code.

我不清楚您的文本框的名称是上面代码示例中的“TextBox1”还是“txtNewNameHere”,所以我在上面的代码中只使用了通用的“Text1”。

If you're unsure of what the Name of your textbox is yourself right click the text box and select 'properties'

如果您不确定自己的文本框名称是什么,请右键单击文本框并选择“属性”

Update: Assumed that textbox was located in a form object.

更新:假设文本框位于表单对象中。

回答by mkingston

This will clear the text box on focus if it did not already have focus:

如果它还没有焦点,这将清除焦点上的文本框:

Private Sub TextBox1_Enter()
    TextBox1.Value = ""
End Sub

It will not clear the text box every time it is clicked. Does that help?

不会在每次点击时清除文本框。这有帮助吗?

回答by José Eduardo Costa Lima Vilela

Even after all this time, this is one of the main results in google, so I'd like to share information about the matter that might make things easier for people who could reach the page in the future after a couple of hours on the matter:

即使过了这么久,这也是谷歌的主要结果之一,所以我想分享有关此事的信息,这些信息可能会让未来几个小时后可以访问该页面的人更轻松:

The method below works only for unbounded controls:

以下方法仅适用于无界控件:

Private Sub Text1_GotFocus()
If Me.Text1.Value = "First" Then
    Me.Text1.Value = ""
End If 
End Sub 

What I did was to create a function that goes through all controls, checks if they are bounded, and if not bounded, attributes the default value:

我所做的是创建一个遍历所有控件的函数,检查它们是否有界,如果没有界,则属性为默认值:

NOTES ON THE FUNCTION BELOW: 1- I think "DefaultValue" is better then "", but that would work too. 2- The "Select case" is needed otherwise the .ControlSource property is not found. 3- You could check the bound of the control in each type if needed, I only had textboxes with bound. 4- The "Me.Refresh" makes bounded controls to be set to default, as long the bound is related to controls in the same form.

关于以下功能的说明: 1- 我认为“DefaultValue”比“”更好,但这也可以。2- 需要“选择案例”,否则找不到 .ControlSource 属性。3- 如果需要,您可以检查每种类型的控件的边界,我只有带边界的文本框。4-“Me.Refresh”使有界控件设置为默认值,只要绑定与同一表单中的控件相关即可。

Function ClearControls(frm As Form)
Dim ctrl As Control
For Each ctrl In frm.Controls
    Select Case ctrl.ControlType
            Case acTextBox
                If ctrl.ControlSource = Null Then
                    ctrl.Value = ctrl.DefaultValue
                End If
            Case acOptionGroup, acComboBox, acListBox
                ctrl.Value = Null
            Case acCheckBox
                ctrl.Value = False
        End Select
Next
Me.Refresh
End Sub

So, on the click event code I just called the ClearControls function as follows:

因此,在单击事件代码中,我只是调用了 ClearControls 函数,如下所示:

ClearControls Me

This also makes possible to re-use the function in other controls/events.

这也使得在其他控件/事件中重新使用该功能成为可能。