Text Box Clear User Instructions

Ziggy12

Active Member
Joined
Jun 26, 2002
Messages
361
How do I clear a text box of user instructions when they click in the text box or start entering into it.
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962
Put your instructions in the TextBox as either the default or in the UserForm_Initialize event. Then put the following code for your TextBox:

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X AS Single, VyVal Y As Single)

With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
End With
Selection.ClearContents
End Sub

Did that do the trick
 

Ziggy12

Active Member
Joined
Jun 26, 2002
Messages
361
Excellent. How do I give focus to something else in the userform so the user has to click into it?
 

Ziggy12

Active Member
Joined
Jun 26, 2002
Messages
361

ADVERTISEMENT

After initialisation of the user form the cursor is in textbox1 ready to type. Unles the user moves the mouse down the prompt text in textbox1 is not selected and deleted. Thought the best way to deal with that is give something else on the user form the focus so the user has to move the cursor to the text box. Trouble is I haven't been able to find the function that gives an oject on the userform focus. I know it's in there somewhere.
 

Ziggy12

Active Member
Joined
Jun 26, 2002
Messages
361
Got it thanks.
Public Started as Boolean

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
TextBox1.Tag = "Received"
If Started = False Then
With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
End With
Selection.ClearContents
End If
Started = True
End Sub

and it's commandbutton1.setfocus

thanks for your help
 

Ziggy12

Active Member
Joined
Jun 26, 2002
Messages
361
Wondered why the currently selected cell would mysteriously clear contents. The clear contents in the code above does more than clear the selection text in the text box.

best way to do above is:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
TextBox1.Tag = "Received"
If Started = False Then
With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
End With
'Selection.ClearContents
TextBox1.EnterFieldBehavior = _
fmEnterFieldBehaviorRecallSelection
End If
Started = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,307
Messages
5,600,869
Members
414,411
Latest member
Snowmanaus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top