Control.OnTextChanged(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.
TextChanged Zgłasza zdarzenie.
protected:
virtual void OnTextChanged(EventArgs ^ e);
protected virtual void OnTextChanged(EventArgs e);
abstract member OnTextChanged : EventArgs -> unit
override this.OnTextChanged : EventArgs -> unit
Protected Overridable Sub OnTextChanged (e As EventArgs)
Parametry
Przykłady
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 OnTextChanged 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 OnTextChanged(EventArgs) w klasie pochodnej należy wywołać metodę klasy OnTextChanged(EventArgs) bazowej, aby zarejestrowani delegaci odbierali zdarzenie.