Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear text in textbox upon clicking it

Tags:

vba

textbox

I have text in a textbox, which says "NEW NAME HERE"

Is it possible with in VBA to clear this text as soon as the user clicks in that textbox?

If so could anyone please let me know how?

THANKS-

like image 296
maple Avatar asked Nov 01 '10 11:11

maple


People also ask

What is the code to clear the content of TextBox?

Clear() or TextBox. Text = string. Empty.

How do you clear TextBox in C after submit?

You may use JavaScript form reset() method or loop throw all textboxes and set Text property to empty string. Save this answer.

How do you clear in Visual Basic?

Use Clear to explicitly clear the Err object after an error has been handled, for example, when you use deferred error handling with On Error Resume Next. The Clear method is called automatically whenever any of the following statements is executed: Any type of Resume statement. Exit Sub, Exit Function, Exit Property.


2 Answers

Assuming the TextBox is named TextBox1, the following should work

Private Sub TextBox1_GotFocus()
    TextBox1.Text = ""
End Sub
like image 104
Adam Ralph Avatar answered Sep 29 '22 09:09

Adam Ralph


I know it's an old post, but I just stumbled upon it, and thought I'd add an answer. Maybe someone will find it useful some day :-)

I would check the current value before clearing the content. That way it clears the content when you first click it, but if you click it again later (to edit the name perhaps) it does nothing, allowing edits without having to start over.

Assuming the TextBox is named TextBox1 and the placeholder text is NEW NAME HERE, the following should work

Private Sub TextBox1_Enter()
    If TextBox1.Text = "NEW NAME HERE" Then
        TextBox1.Text = ""
    End If
End Sub

To restore the placeholder text if the textbox is left empty:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Trim(TextBox1.Text) = "" Then
        TextBox1.Text = "NEW NAME HERE"
    End If
End Sub

Tested in Word 2010 VBA

like image 40
Gertsen Avatar answered Sep 29 '22 08:09

Gertsen