Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# User-controls UI
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 21-Oct-12 2:04am
Edited 21-Oct-12 2:46am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the Enabled[^] property to allow or prevent editing.
  Permalink  
Comments
user_code at 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 at 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 at 21-Oct-12 8:04am
   
Focus works great. Thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try below code.
//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
  Permalink  
Comments
user_code at 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)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 21 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100