不使用输入控件更新文本框
我有一个 Windows 窗体应用程序,它由一堆控件组成,更具体地说,是两个 textBox
。其中之一是只读的。只读的 textBox
值应该与用户可以输入的 textBox
相同。
因此,如果用户在 textBox
A 中键入“Hello World”,则 textBox
B 中的值应自动更新为“Hello World”。
我该怎么做呢?我知道我只需要设置文本值,我只是不确定将代码放在哪里来自动完成,而不是在单击按钮或类似的操作时执行。
I have a windows form application which consists of a bunch of controls, but more specifically, two textBoxes
. One of them is read only. The read only textBox
value is supposed to be the same as the textBox
that the user can type into.
So if the user types "Hello World" into textBox
A, the value in textBox
B should be automatically updated to "Hello World".
How do I go about doing this? I know I just need to set the text values, I'm just not sure where I place the code to get it done automatically rather than executed when a button is click or something along those lines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
文本更改事件:
TextChanged event:
听起来您想要类似的东西:
换句话说,每当一个文本框中的文本发生更改时,请更新另一个文本框中的文本。这使用
Control.TextChanged
事件。
It sounds like you want something like:
In other words, whenever the text in one textbox changes, update the other. This uses the
Control.TextChanged
event.如果您希望在 textBoxA 的文本更改后(即用户在 textBoxA 中按下某个键后立即)更新 textBoxB,则事件为 TextChanged:
如果您希望仅在用户完成编辑后更新 textBoxB 中的文本textBoxA,您应该使用离开事件:
If you want textBoxB to be updated as soon as the text of textBoxA is changed (i.e immediately after the user press a key in textBoxA) the event is TextChanged:
If you prefer to update the text in textBoxB only after the user has finished to edit textBoxA, you should use the Leave event:
这应该可以满足您的需要:
This should do what you need:
比事件方法更短(更好?)的是使用 winform 的数据绑定。只需在
InitializeComponents
调用之后立即使用它即可:Even shorter (better?) than the event approach is using winform's databinding. Just use this right after the
InitializeComponents
call: