473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to clear default text on textbox when I click directly on the textbox?

18
Hello, I'm a beginner of using C#.

I'm creating a window application,
May I know how to clear the default text on textbox when I click directly on the textbox?
For example, the txtBox has the words "[Insert your text here.]", so how to make the words all clear when user click on the textbox ?


Sorry for disturbing you all, can someone provide me the codes please?

Thanks,
by Aeris
Dec 12 '09 #1

✓ answered by tlhintoq

If you check the events for the text box (yellow lightning bold in the Properties pallet) you will see there is a 'Click' event. If you double-click in value space for that event, Visual Studio will stub out a handler for that event that looks something like this (give or take your TextBox's name)
Expand|Select|Wrap|Line Numbers
  1.         private void tbMyTextBox_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.         }
  5.  
Now that you have a handler method for the Click event you can do what you like, such as clearing the text.
)
Expand|Select|Wrap|Line Numbers
  1.         private void tbMyTextBox_Click(object sender, EventArgs e)
  2.         {
  3.            tbMyTextBox.Text = string.empty;
  4.         }
  5.  
Let me point something out though... There is more than one way to enter a text box. You can click in it with a mouse... but you can also tab to it from another control. If you only handle the Click event, then your textbox won't clear if someone tabs into it. If you want to handle either case you might want to worry about the 'Enter' event instead of the 'Click' event.

6 52877
tlhintoq
3,525 Expert 2GB
If you check the events for the text box (yellow lightning bold in the Properties pallet) you will see there is a 'Click' event. If you double-click in value space for that event, Visual Studio will stub out a handler for that event that looks something like this (give or take your TextBox's name)
Expand|Select|Wrap|Line Numbers
  1.         private void tbMyTextBox_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.         }
  5.  
Now that you have a handler method for the Click event you can do what you like, such as clearing the text.
)
Expand|Select|Wrap|Line Numbers
  1.         private void tbMyTextBox_Click(object sender, EventArgs e)
  2.         {
  3.            tbMyTextBox.Text = string.empty;
  4.         }
  5.  
Let me point something out though... There is more than one way to enter a text box. You can click in it with a mouse... but you can also tab to it from another control. If you only handle the Click event, then your textbox won't clear if someone tabs into it. If you want to handle either case you might want to worry about the 'Enter' event instead of the 'Click' event.
Dec 12 '09 #2
aeris
18
Billion thanks so much!

My problem is solved. thanks so much, tlhintoq.

By Aeris.
Dec 12 '09 #3
aeris
18
But then, I realised that the textbox cannot be inserted text anymore. How to make it enable to insert text after the click event of clear text?

Thanks !!
Dec 12 '09 #4
MrMancunian
569 Expert 512MB
Use an If...Then-statement to check if the value of your textbox is the same als the value of your default text. Only if that results in True you want the textbox to clear.

Expand|Select|Wrap|Line Numbers
  1. private void tbMyTextBox_Click(object sender, EventArgs e) 
  2.   If(tbMyTextBox.Text == "[Insert your text here.]")
  3.   {
  4.     tbMyTextBox.Text = string.empty;
  5.   }
  6. }
Steven
Dec 12 '09 #5
aeris
18
Thanks Steven so so much. It's work!
Billion thanks! :)
Dec 12 '09 #6
Bogu
1
Similarly, for Tab entry:
go to Form Properties ^ Events ^ AcceptsTabChanged;
select the same tbMyTextBox_Click
Oct 23 '19 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: msnews.microsoft.com | last post by:
Hello! I'm Jim by asp How can show "abc" in textbox when click one botton?
0
by: Tetet B via .NET 247 | last post by:
I'm using VB.Net and trying to retrieve the data in the textboxcell of a datagrid after a successful display (used Databind).User can either display data or enter data in the textbox. I canalways get...
4
by: MA | last post by:
Hi all! I have a problem here. I have a web page for user administration. On this page I have a textbox for password. This textbox are in TextMode password. But when I get data, via a data...
5
by: Kemp | last post by:
I need help with clearing the text boxes when the submit button is clicked. I am developing a web application. I have tried placing the following code in the button_click event, but this doesn't...
0
by: CCLeasing | last post by:
Hello, I have searched google but can not find a straight forward answer to my problem. Hopefuly someone will be kind enough to offer their expertise. Please forgive if this seems a bit convoluted...
4
by: Surek | last post by:
I have created a form using the wizard, to display the contents of the table. I need to update the table by entering the data through the form. When i click the update button of my form, it ignores...
4
by: aeris | last post by:
Hello! I'm using C# to doing my window application. May I ask, how to write text on textbox after the default text is clear? I have setted the default text in textbox, when a user click on it,...
5
by: hidayu1986 | last post by:
how to disable textbox when click on radiobutton using VB script on Visual Studio.Net 2005. currently i used this coding but it is not functioning. Private sub...
10
by: JoeKid09 | last post by:
Hi All, Thanks you in advance for your help. I am working on a form that has text box for Budget information, combo box for location and list box for population which is related to locations combo...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.