Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a user control in c# that consists a textbox and an "Edit" button.
When the user presses the "Edit" button, I want the textbox to change to a mode that it is waiting for the user to enter the text (exactly as a double click on the text box acts).

When the user finished his edit, and presses the Enter key in the keyboard, or clicks on another place in my user control, I want the edited text to be kept in the textbox, and then don't let the user edit it until he presses the "Edit" button again.

How can I do it?

More than the Editable property, how can I do it that when the user presses the "Edit" button, the textbox will be highlighted and wait for text to be entered (I want to action of the "Edit" button to be exactly as the default action of a double-click on the textbox).

Thanks
Posted
Updated 21-Oct-12 1:46am
v2

Use the Enabled[^] property to allow or prevent editing.
 
Share this answer
 
Comments
user_code 21-Oct-12 7:44am    
Thanks, but more than the Enabled property, how can I do it that when the user presses the "Edit" button, the textbox will be highlighted immediately and wait for text? (I want to action of the "Edit" button to be exactly as the default action of a double-click on the textbox).
Richard MacCutchan 21-Oct-12 7:50am    
Then you could use the Focus() method. Try and make use of the documentation to see what methods and properties are available in the controls you are using.
user_code 21-Oct-12 8:04am    
Focus works great. Thanks!
Try below code.
C#
//Tbx is ur textbox
//btn is ur edit button

//this line would make textbox readonly ,add it to ur Form Constructor
tbx.ReadOnly=true;
//this line would make textbox editable and focuses it when edit button is clicked
btn.Click+=new EventHandler((o,e)=>{tbx.ReadOnly=False;tbx.Focus();})
//this line would make textbox readonly when the enter key(return key) is pressed
tbx.KeyUp+=new KeyEventHandler((o,e)=>{if(e.KeyCode==Keys.Return)tbx.ReadOnly=true;});
//this line would make textbox readonly,when textbox loses focus
tbx.Leave += new EventHandler((o, e)=>tbx.ReadOnly=true);


you can use Enabled property instead of Readonly property
 
Share this answer
 
Comments
user_code 21-Oct-12 9:40am    
Thanks!
How can I remove the focus? For example, if I have another button of "Stop Edit".

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