Control.TextChanged 事件

Text 属性值更改时发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event TextChanged As EventHandler
用法
Dim instance As Control
Dim handler As EventHandler

AddHandler instance.TextChanged, handler
public event EventHandler TextChanged
public:
event EventHandler^ TextChanged {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
/** @event */
public void add_TextChanged (EventHandler value)

/** @event */
public void remove_TextChanged (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

如果通过编程修改或用户交互更改 Text 属性,则将引发此事件。

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例更改显示货币数据的 TextBoxForeColor。该示例将文本转换成十进制数,并且如果该数为负数,则将 ForeColor 更改为 Color.Red;如果该数为正数,则更改为 Color.Black。此示例要求您有一个包含 TextBoxForm

Private Sub currencyTextBox_TextChanged(sender As Object, _ 
  e As EventArgs) Handles currencyTextBox.TextChanged
   Try
      ' Convert the text to a Double and determine if it is a negative number.
      If Double.Parse(currencyTextBox.Text) < 0 Then
         ' If the number is negative, display it in Red.
         currencyTextBox.ForeColor = Color.Red
      Else
         ' If the number is not negative, display it in Black.
         currencyTextBox.ForeColor = Color.Black
      End If
   Catch
      ' If there is an error, display the text using the system colors.
      currencyTextBox.ForeColor = SystemColors.ControlText
   End Try
End Sub 
private void currencyTextBox_TextChanged(object sender, EventArgs e)
{
   try
   {
      // Convert the text to a Double and determine if it is a negative number.
      if(double.Parse(currencyTextBox.Text) < 0)
      {
         // If the number is negative, display it in Red.
         currencyTextBox.ForeColor = Color.Red;
      }
      else
      {
         // If the number is not negative, display it in Black.
         currencyTextBox.ForeColor = Color.Black;
      }
   }
   catch
   {
      // If there is an error, display the text using the system colors.
      currencyTextBox.ForeColor = SystemColors.ControlText;
   }
}
private:
   void currencyTextBox_TextChanged( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      try
      {
         // Convert the text to a Double and determine if it is a negative number.
         if ( Double::Parse( currencyTextBox->Text ) < 0 )
         {
            // If the number is negative, display it in Red.
            currencyTextBox->ForeColor = Color::Red;
         }
         else
         {
            // If the number is not negative, display it in Black.
            currencyTextBox->ForeColor = Color::Black;
         }
      }
      catch ( Exception^ ) 
      {
         // If there is an error, display the text using the system colors.
         currencyTextBox->ForeColor = SystemColors::ControlText;
      }
   }
private void currencyTextBox_TextChanged(Object sender, EventArgs e)
{
    try {
        // Convert the text to a Double and determine if it is a 
        // negative number.
        if (System.Double.Parse(currencyTextBox.get_Text()) < 0) {
            // If the number is negative, display it in Red.
            currencyTextBox.set_ForeColor(Color.get_Red());
        }
        else {
            // If the number is not negative, display it in Black.
            currencyTextBox.set_ForeColor(Color.get_Black());
        }
    }
    catch (System.Exception exp) {
        // If there is an error, display the text using the system colors.
        currencyTextBox.set_ForeColor(SystemColors.get_ControlText());
    }
} //currencyTextBox_TextChanged

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
OnTextChanged
Control.Text 属性