Control.ForeColorChanged 事件
在 ForeColor 属性值更改时发生。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Event ForeColorChanged As EventHandler
用法
Dim instance As Control
Dim handler As EventHandler
AddHandler instance.ForeColorChanged, handler
public event EventHandler ForeColorChanged
public:
event EventHandler^ ForeColorChanged {
void add (EventHandler^ value);
void remove (EventHandler^ value);
}
/** @event */
public void add_ForeColorChanged (EventHandler value)
/** @event */
public void remove_ForeColorChanged (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。
备注
如果通过编程修改或通过交互更改 ForeColor 属性,则将引发此事件。
有关处理事件的更多信息,请参见 使用事件。
示例
下面的代码示例是一个事件处理程序,该处理程序在 Text 属性值更改时执行。Control 类有几个名称模式为 PropertyNameChanged 的方法,它们在相应的 PropertyName 值(PropertyName 表示相应的属性名)更改时引发。
下面的代码示例更改显示货币数据的 TextBox 的 ForeColor。该示例将文本转换成十进制数,并且如果该数为负数,则将 ForeColor 更改为 Color.Red;如果该数为正数,则更改为 Color.Black。该示例要求有一个包含 TextBox 的 Form。
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
请参见
参考
Control 类
Control 成员
System.Windows.Forms 命名空间
OnForeColorChanged
Control.ForeColor 属性
Color