Control.OnFontChanged(EventArgs) Metoda
Definicja
Ważny
Niektóre informacje dotyczą produktów przedpremierowych, które mogą zostać znacznie zmodyfikowane przed premierą. Microsoft nie udziela żadnych gwarancji, ani wyraźnych, ani domniemanych, dotyczących informacji podanych tutaj.
FontChanged Zgłasza zdarzenie.
protected:
virtual void OnFontChanged(EventArgs ^ e);
protected virtual void OnFontChanged(EventArgs e);
abstract member OnFontChanged : EventArgs -> unit
override this.OnFontChanged : EventArgs -> unit
Protected Overridable Sub OnFontChanged (e As EventArgs)
Parametry
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 OnFontChanged umożliwia również klasom pochodnym obsługę zdarzenia bez dołączania delegata. Jest to preferowana technika obsługi zdarzenia w klasie pochodnej.
Notatki dla spadkobierców
Podczas zastępowania OnFontChanged(EventArgs) w klasie pochodnej należy wywołać metodę klasy OnFontChanged(EventArgs) bazowej, aby zarejestrowani delegaci odbierali zdarzenie.