如何获取TextChanged中的新文本? [英] How to get the NEW text in TextChanged?

查看:145
本文介绍了如何获取TextChanged中的新文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文本框中我正在监视文本更改。在做一些事情之前,我需要检查文本。但是我现在只能检查一下旧的文字。如何获取新的文本?

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 ?

private void textChanged(object sender, EventArgs e)
{
    // need to check the new text
}

我知道.NET Framework 4.5有新的 TextChangedEventArgs 类,但我必须使用.NET Framework 2.0。

I know .NET Framework 4.5 has the new TextChangedEventArgs class but I have to use .NET Framework 2.0.

推荐答案

p> 获取新值

您可以使用 Text 文本框。如果此事件用于多个文本框,那么您将需要使用发件人参数来获取正确的 TextBox 控件,如此...

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...

private void textChanged(object sender, EventArgs e)
{
    TextBox textBox = sender as TextBox;
    if(textBox != null)
    {
        string theText = textBox.Text;
    }
}






获取OLD值

对于那些想要获得旧值的用户,您需要自己保留。我建议一个简单的变量开始为空,并在每个事件结束时更改:

For those looking to get the old value, you will need to keep tract 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;
    }
}

您可以创建自己的TextBox控件,内置的一个,并添加这个附加功能,如果你打算使用这么多。

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.

这篇关于如何获取TextChanged中的新文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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