共用方式為


Control.ForeColorChanged 事件

定義

發生在 ForeColor 屬性值變更時。

public:
 event EventHandler ^ ForeColorChanged;
public event EventHandler ForeColorChanged;
public event EventHandler? ForeColorChanged;
member this.ForeColorChanged : EventHandler 
Public Custom Event ForeColorChanged As EventHandler 

事件類型

EventHandler

範例

下列程式碼範例是屬性值變更時 Text 所執行的事件處理常式。 當對應的 PropertyName 值變更 (PropertyName 代表對應屬性的名稱) 時,類別 Control 有數個名稱模式 PropertyName Changed 所引發的方法。

下列程式碼範例會變更 ForeColor TextBox 顯示貨幣資料的 。 此範例會將文字轉換成十進位數,如果數位為負數,則將 變更為 Color.Black ,如果數位為正數,則為 。 ForeColor Color.Red 此範例需要您有 Form 包含 的 TextBox

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

備註

如果屬性是由程式設計修改或透過互動來變更, ForeColor 就會引發這個事件。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱