Removing default textbox data on click

  • Thread starter Thread starter Jane Sharpe
  • Start date Start date
J

Jane Sharpe

Hi,

I have a textbox with the words "Enter your name here" inserted as default
text - At the moment, to remove this the user must highlight all the text
and delete before they type in their name - I've seen sites where all this
text dissapears as soon as the user clicks on it - how do I do that ?

Thanks

Jane
 
While I probably wouldn't normally hardcode a string literal, this should
give you the idea to get started.

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

Or, to keep the default text there until they start typing:
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.SelectAll()
End If
End Sub
 
Thanks, I was going to ask for a pointer..... now I'm going to ask for
another !

I get this error...

Handles clause requires a WithEvents variable

Jane
 
To create the "GotFocus" handler automatically, do this: drag your textbox
to the form, and give it a name (in the designer). The default name will be
TextBox1 for the first text box.

Go to the code view, and in the left-hand dropdown just above the code,
select "TextBox1", or whatever you named it. Then in the righ-hand
dropdown, select the "GotFocus" item (it will have a little lightning bolt
next to it). This will create the Shell of the event handler.

In the code I sent, it assumes that the text box was named TextBox1. If it
wasnt, change the part that says
.... "Handles TextBox1.GotFocus "
To "Handles TheRealNameOfMyTextBox.GotFocus".

By named, I mean this : in the designer of your form, select the text box.
Look in the properties window and find the "(name)" property. Whatever is
entered there is the "name" of the text box that I keep referring to.
 
And there probably lies my problem, I'm not developing in studio, just
writing code !
 
when defining your textbox add withevents keyword:
private WithEvents myTextbox As TextBox


other solution:

private myTextbox As TextBox
Public Sub New()
addHandler myTextbox.GotFocus, AddressOf TextBox1_GotFocus
End Sub

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs)
....
End Sub


or something that looks alike ;-)
 
Thanks for all this help, but I just CANT get this all to gel.

With this test code, I get the error

BC30590: Event 'GotFocus' cannot be found.


Heres my test code:-

<%@ Page Language="VB" %>

<script runat="server">

Private WithEvents TextBox1 As TextBox
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

</script>

<html>

<body>
<form Id="frmForm1" runat="server">
Enter your name here</asp:TextBox>
</p>
</form>
</body>
</html>
 
hum
you forgot to specify one little detail
it's a webapplication...

try this group: microsoft.public.dotnet.framework.aspnet
 
AhHa! it's a web application. That makes it a different beast:
The GotFocus event doesn't really make sense in a web application, since the
events in your VB.NET code are fired on the server side, not the client
side. To do this, you need to use some javascript.

Try this instead:

<%@ Page Language="VB" %>
<HTML>
<script runat="server">

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
TextBox1.Attributes.Add("onFocus", "removeText('" & TextBox1.ClientId &
"');")
End Sub

</script>

<script language=javascript>
function removeText( clientId)
{
document.getElementById( clientId).value = '';
}
</script>
<body>
<form Id="frmForm1" runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" BorderStyle="Solid">
Enter your name here</asp:TextBox>
</p>
</form>
</body>
</HTML>
 
Did I not mention that little detail, my apologies, its confusing enough
finding the right newsgroup !!

Anyway, I'm working now, so Thank You, Thank You, Thank You !!

Jane
 
Back
Top