Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to get a focus on a textbox,when it get focus the default text shouldn't be removed until the user enter the data in it.how can i do this?
i have a textbox whose text property is set to "first name".when this textbox is focused then the default text which is "first name"shouldn't be removed until user enter his name.
Posted
Updated 8-Aug-14 8:47am
v2
Comments
SteveyJDay 8-Aug-14 14:13pm    
Is this a Winforms or ASP.NET question?
Nainaaa 8-Aug-14 14:42pm    
winforms
OriginalGriff 8-Aug-14 14:15pm    
Depends on a lot of things: like what environment this is to work in, when the text box is to get the focus, and what exactly you mean by "the default text shouldn't be removed".
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
[no name] 8-Aug-14 15:26pm    
Use a textbox control that supports a water mark like you have already been told.

In Winforms, hook the GotFocus event. In XAML, create a behavior and attach it to the GotFocus event. Either way, the function should set the "SelectedPosition" property to 0 (or 1... I can't remember which off the top of my head) and the "SelectedLength" property to the length of the text property. That will select the text in the text box when the method is fired.

In the future, please include the important details like which technology you are working in. Tagging the question with C# isn't a whole lot to go on.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Aug-14 14:58pm    
Why "GotFocus"? The question is about loosing focus... Please see my answer.
—SA
You cannot prevent the user from moving focus away from any of your control. Even if you could, it would be a great malicious act against the user, who has every right to focus whatever control in the whole system; nobody would use your projects again if you had done such tricks.

Instead, you can capture the event of loosing the focus (ignoring the cases when the focus is lost due to activation of other windows, perhaps the windows of other applications) and, say, show some error condition (incomplete or invalid data). This is how, for example:
C#
myEditBox.LostFocus += (sender, eventArgs) => { /* do something about it */ };

Please see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus(v=vs.110).aspx[^].

—SA
 
Share this answer
 
You cannot prevent the user from moving focus away from any of your controls. Even if you could, it would be a great malicious act against the user, who has every right to focus whatever control in the whole system; nobody would use your projects again if you had done such tricks.

Instead, you can capture the event of loosing the focus (ignoring the cases when the focus is lost due to activation of other windows, perhaps the windows of other applications) and, say, show some error condition (incomplete or invalid data). This is how, for example:
C#
myEditBox.LostFocus += (sender, eventArgs) => { /* do something about it */ };

Please see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus(v=vs.110).aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900