首页 > 解决方案 > 仅在第一次时在 MouseDown/MouseUp 上清空预填充的 TextBox

问题描述

我是 word-vba 的新手(只是为了让你知道我的问题可能真的很愚蠢)。

我只想在第一次单击文本框时清除文本框。

我试过 For... Next 但我无法正确配置它

Private Sub SWName_Field_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
SWName_Field.Text = ""
End Sub

我希望代码的工作方式与它的工作方式完全相同,但是当我输入一些文本时,例如用户犯了错误或拼写错误,文本框中的第二次点击不应该清除里面的文本。

谢谢你的支持

标签: vbams-word

解决方案


任何 UserForm 控件中都没有内置的活动状态标识符。因此,您需要使用元数据来指定和识别您的 mousedown 是否是第一次发生。

为此使用Tag控件的属性。

有关详细信息,请参阅代码注释。

Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    '/ Use the Tag field to determine and store the state of Text Box.
    If Len(Me.TextBox1.Tag) < 1 Then
        '/ If Mousedown for the very first  time then TextBox's tag is empty.
        '/ Go ahead, clean the textbox.
        '/ And set a text in tag.
        Me.TextBox1.Text = ""
        Me.TextBox1.Tag = "Text Cleared"
    End If

End Sub

推荐阅读