Control.OnBackColorChanged(EventArgs) Metoda

Definicja

BackColorChanged Zgłasza zdarzenie.

protected:
 virtual void OnBackColorChanged(EventArgs ^ e);
protected virtual void OnBackColorChanged (EventArgs e);
abstract member OnBackColorChanged : EventArgs -> unit
override this.OnBackColorChanged : EventArgs -> unit
Protected Overridable Sub OnBackColorChanged (e As EventArgs)

Parametry

e
EventArgs

Element EventArgs zawierający dane zdarzenia.

Przykłady

Poniższy przykład kodu to metoda wywoływana zdarzeń wykonywana po Text zmianie wartości właściwości. Klasa Control ma kilka metod o nazwie PropertyName, On które zgłaszają odpowiednie zdarzenie PropertyNameChangedChanged, gdy wartość PropertyName zmieni się (PropertyName reprezentuje nazwę odpowiedniej właściwości).

Poniższy przykład kodu zmienia ForeColor klasę pochodną TextBox wyświetlającą dane walutowe. W przykładzie tekst jest konwertowany na liczbę dziesiętną i zmienia ForeColor wartość na Color.Red , jeśli liczba jest ujemna, a jeśli Color.Black liczba jest dodatnia. W tym przykładzie jest wymagana klasa, która pochodzi z TextBox klasy .

protected:
   virtual void OnTextChanged( System::EventArgs^ e ) override
   {
      try
      {
         // Convert the text to a Double and determine
         // if it is a negative number.
         if ( Double::Parse( this->Text ) < 0 )
         {
            // If the number is negative, display it in Red.
            this->ForeColor = Color::Red;
         }
         else
         {
            // If the number is not negative, display it in Black.
            this->ForeColor = Color::Black;
         }
      }
      catch ( Exception^ ) 
      {
         // If there is an error, display the
         // text using the system colors.
         this->ForeColor = SystemColors::ControlText;
      }

      TextBox::OnTextChanged( e );
   }
protected override void OnTextChanged(System.EventArgs e)
{
   try
   {
      // Convert the text to a Double and determine
      // if it is a negative number.
      if(double.Parse(this.Text) < 0)
      {
         // If the number is negative, display it in Red.
         this.ForeColor = Color.Red;
      }
      else
      {
         // If the number is not negative, display it in Black.
         this.ForeColor = Color.Black;
      }
   }
   catch
   {
      // If there is an error, display the 
      // text using the system colors.
      this.ForeColor = SystemColors.ControlText;
   }
   
   base.OnTextChanged(e);
}
Protected Overrides Sub OnTextChanged(e As System.EventArgs)
   Try
      ' Convert the text to a Double and determine
      ' if it is a negative number.
      If Double.Parse(Me.Text) < 0 Then
         ' If the number is negative, display it in Red.
         Me.ForeColor = Color.Red
      Else
         ' If the number is not negative, display it in Black.
         Me.ForeColor = Color.Black
      End If
   Catch
      ' If there is an error, display the
      ' text using the system colors.
      Me.ForeColor = SystemColors.ControlText
   End Try

   MyBase.OnTextChanged(e)
End Sub

Uwagi

Podnoszenie zdarzenia wywołuje program obsługi zdarzeń przez delegata. Aby uzyskać więcej informacji, zobacz Obsługa i podnoszenie zdarzeń.

Metoda OnBackColorChanged umożliwia również klasom pochodnym obsługę zdarzenia bez dołączania delegata. Jest to preferowana technika obsługi zdarzenia w klasie pochodnej.

Uwagi dotyczące dziedziczenia

Podczas zastępowania OnBackColorChanged(EventArgs) w klasie pochodnej należy wywołać metodę klasy OnBackColorChanged(EventArgs) bazowej, aby zarejestrowani delegaci odbierali zdarzenie.

Dotyczy

Zobacz też