仅在按下按钮时如何编辑c#文本框? [英] How to edit a c# textbox only when a button is pressed?

查看:126
本文介绍了仅在按下按钮时如何编辑c#文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在c#中有一个用户控件,其中包含一个文本框和一个编辑"按钮.
当用户按下编辑"按钮时,我希望文本框更改为等待用户输入文本的模式(恰好是双击文本框的行为).

当用户完成编辑并按键盘上的Enter键,或单击我的用户控件中的另一个位置时,我希望将编辑后的文本保留在文本框中,然后不要让用户对其进行编辑,直到他再次按下编辑"按钮.

我该怎么办?

除了Editable属性之外,我该怎么做,当用户按下"Edit"按钮时,文本框将突出显示,并等待输入文本(我希望"Edit"按钮的操作与双击文本框的默认操作).

谢谢

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

推荐答案

使用 ^ ]属性以允许或禁止进行编辑.
Use the Enabled[^] property to allow or prevent editing.


尝试下面的代码.
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);



您可以使用Enabled属性代替Readonly属性



you can use Enabled property instead of Readonly property


这篇关于仅在按下按钮时如何编辑c#文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆