Avatar of karel_jespers
karel_jespers

asked on 

Start sub when textbox on my form is selected

When to focus is put on a textbox on my form,  i want to start a sub.
This sub must erase the default text in the textbox telling what data you need to put there.

I cant find an event to do this job.
Can anyone tell me how to do this.
Tks
.NET Programming

Avatar of undefined
Last Comment
karel_jespers
Avatar of existenz2
existenz2
Flag of Netherlands image

You could do that best with javascript and the onfocus event:

<script language='JavaScript'>
function ClearText(id) {
   var textbox = document.getElementById(id);
   textbox.value = "";
}
</script>

<asp:TextBox id="sample" onfocus="ClearText('sample')"></asp:TextBox>
Avatar of sreeganesh
sreeganesh

u want to do this in windows application or web?
If in windows app this will work. Use the enter event of textbox
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
      Call validatetext()
End Sub
Sub validatetext()
      TextBox1.Text = ""
      MessageBox.Show("Enter valid data")
End Sub

Cheers!!
Happy programming
Avatar of karel_jespers

ASKER

hi sreeganesh,
it is a web application (forgot to tell you) and the .enter event is not allowed there


existenz2,
it must be coded in behind code, a lot of other programming must be placed around it
Avatar of sreeganesh
sreeganesh


I think this code will suit ur need.
I was not sure wht type off app. thts y given the previous code. Could u try this.
private void Page_Load(object sender, System.EventArgs e)
{
      // Put user code to initialize the page here
      txtTest.Attributes.Add("onFocus","ClearTextbox()");
      StringBuilder sb = new StringBuilder();
      sb.Append("<script language=\"javascript\">\n");
      sb.Append("function ClearTextbox() {\n");
      sb.AppendFormat("eval(\"document.forms[0].{0}.value = \\'\\'\");\n", tTest.ClientID);
      sb.Append("alert('Enter a valid data');}\n");
      sb.Append("</script>");
      Page.RegisterClientScriptBlock("ClearTextbox",sb.ToString());

}
Avatar of sreeganesh
sreeganesh

Small correction
sb.AppendFormat("eval(\"document.forms[0].{0}.value = \\'\\'\");\n", tTest.ClientID);
In this line instead of tTest.ClientId
use
txtTest.Client ID

Also include
System.Text namespace
Avatar of karel_jespers

ASKER

sreeganesh
it is a vb.net application
Avatar of sreeganesh
sreeganesh

OK  VB.NET WEB APPLICATION
Here goes the code try it

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            txtTest.Attributes.Add("onFocus", "ClearTextbox();")
            Dim sb As New StringBuilder
            'StringBuilder sb = new StringBuilder();
            sb.Append("<script language=javascript> ")
            sb.Append("function ClearTextbox() { ")
            sb.AppendFormat("eval(document.forms[0].{0}.value =' '); ", txtTest.ClientID)
            sb.Append("alert('Enter a valid data');} ")
            sb.Append("</script>")
            Page.RegisterClientScriptBlock("ClearTextbox", sb.ToString())
End Sub

Hope this will suit ur need
Cheers!!
Happy programming
Avatar of karel_jespers

ASKER

sreeganesh
the code works but the alert must not appear

is there a possibility to start the clear just when the textbox is hit without that alert message
tks
Avatar of sreeganesh
sreeganesh

Ok thts simple just remove the line having alert from the javascript code ie
Try the below code

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            txtTest.Attributes.Add("onFocus", "ClearTextbox();")
            Dim sb As New StringBuilder
            'StringBuilder sb = new StringBuilder();
            sb.Append("<script language=javascript> ")
            sb.Append("function ClearTextbox() { ")
            sb.AppendFormat("eval(document.forms[0].{0}.value =' '); ", txtTest.ClientID)
            sb.Append("</script>")
            Page.RegisterClientScriptBlock("ClearTextbox", sb.ToString())
End Sub
Avatar of karel_jespers

ASKER

sreeganesh
i have put the changed code
but now nothing is happening
in the previous code, the text became empty
ASKER CERTIFIED SOLUTION
Avatar of sreeganesh
sreeganesh

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of karel_jespers

ASKER

all right !
that s the way i like it    ehe ehe
tks
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo