C# 如何在 TextChanged 中获取新文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14337057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the NEW text in TextChanged?
提问by Bitterblue
In a TextBox I'm monitoring the text changes. I need to check the text before doing some stuff. But I can only check the old text in the moment. How can I get the new Text ?
在 TextBox 中,我正在监视文本更改。我需要在做一些事情之前检查文本。但我现在只能查看旧文本。我怎样才能得到新的 Text ?
private void textChanged(object sender, EventArgs e)
{
// need to check the new text
}
I know .NET Framework 4.5 has the new TextChangedEventArgs
class but I have to use .NET Framework 2.0.
我知道 .NET Framework 4.5 有新TextChangedEventArgs
类,但我必须使用 .NET Framework 2.0。
采纳答案by musefan
Getting the NEW value
获得新价值
You can just use the Text
property of the TextBox
. If this event is used for multiple text boxes then you will want to use the sender
parameter to get the correct TextBox
control, like so...
你可以只使用Text
的财产TextBox
。如果此事件用于多个文本框,那么您将需要使用该sender
参数来获得正确的TextBox
控件,就像这样......
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
}
}
Getting the OLD value
获取旧值
For those looking to get the old value, you will need to keep track of that yourself. I would suggest a simple variable that starts out as empty, and changes at the end of each event:
对于那些希望获得旧值的人,您需要自己跟踪。我会建议一个简单的变量,它开始为空,并在每个事件结束时更改:
string oldValue = "";
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
// Do something with OLD value here.
// Finally, update the old value ready for next time.
oldValue = theText;
}
}
You could create your own TextBox control that inherits from the built-in one, and adds this additional functionality, if you plan to use this a lot.
如果您打算大量使用它,您可以创建自己的 TextBox 控件,该控件继承自内置控件,并添加此附加功能。
回答by Jason Evans
Have a look at the textbox eventssuch as KeyUp, KeyPress etc. For example:
看看文本框事件,如KeyUp, KeyPress 等。 例如:
private void textbox_KeyUp(object sender, KeyEventArgs e)
{
// Do whatever you need.
}
Maybe these can help you achieve what you're looking for.
也许这些可以帮助你实现你正在寻找的东西。
回答by dutzu
Even with the older .net fw 2.0 you should still have the new and old value in the eventArgs if not in the textbox.text property itself since the event is fired after and not during the text changing.
即使使用较旧的 .net fw 2.0,如果不在 textbox.text 属性本身中,您仍然应该在 eventArgs 中拥有新旧值,因为事件是在文本更改之后而不是在文本更改期间触发的。
If you want to do stuff while the text is being changed then try the KeyUp event rather then the Changed.
如果您想在更改文本时执行某些操作,请尝试使用 KeyUp 事件而不是 Changed。
回答by Nasir Uddin
private void stIDTextBox_TextChanged(object sender, EventArgs e)
{
if (stIDTextBox.TextLength == 6)
{
studentId = stIDTextBox.Text; // Here studentId is a variable.
// this process is used to read textbox value automatically.
// In this case I can read textbox until the char or digit equal to 6.
}
}