PDA

View Full Version : textbox default value



chungtinhlak
03-26-2009, 07:27 AM
I have 28 textbox that allows user to input their keyword to search. The default value for this is "Enter Keyword".

I want to make it so that when they click inside this textbox, the default goes away so they can enter they're new keyword, but if they exit the textbox without entering anything, the default keyword "Enter Keyword comes back up.

I did the first textbox with an if statement: I was wondering if there's a better way to do this than write 56 different if statement.

Thanks


Private Sub TextBox1_Enter()
If TextBox1.Value = "Enter Keyword" Then
TextBox1.Value = ""
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Value = "" Then
TextBox1.Value = "Enter Keyword"
End If
End Sub

GTO
03-26-2009, 08:41 AM
Greetings,

Not sure if any real advantage efficiency wise, as the tests are quick, but if I was to try, maybe (for 4 textboxes and a commandbutton):

Option Explicit

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub TextBox1_Enter()
TextBox_SetVal TextBox1
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox_SetVal TextBox1, True
End Sub

Private Sub TextBox2_Enter()
TextBox_SetVal TextBox2
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox_SetVal TextBox2, True
End Sub

Private Sub TextBox3_Enter()
TextBox_SetVal TextBox3
End Sub

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox_SetVal TextBox3, True
End Sub

Private Sub TextBox4_Enter()
TextBox_SetVal TextBox4
End Sub

Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox_SetVal TextBox4, True
End Sub

Private Function TextBox_SetVal(oTxtBox As MSForms.TextBox, _
Optional Exiting As Boolean = False) As String
If Exiting And oTxtBox.Value = vbNullString Then
oTxtBox.Value = "Enter Keyword"
ElseIf Not Exiting And oTxtBox.Value = "Enter Keyword" Then
oTxtBox.Value = vbNullString
End If
End Function

Private Sub UserForm_Initialize()
Dim ctl As Control
For Each ctl In UserForm1.Controls
If ctl.Name Like "TextBox*" Then ctl.Value = "Enter Keyword"
Next
End Sub


Sometimes .Tag is a handy way to pick a control, but what I think I have brain-faded on is that I thought there was a .Type or similar??? (Hopefully if so, someone will smack me back awake)

Anyways, hope this helps a bit and have a great day,

Mark

chungtinhlak
03-26-2009, 08:50 AM
thanks for you help, i'll explore this a bit more.