Control.OnVisibleChanged(EventArgs) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Genera l'evento VisibleChanged.
protected:
virtual void OnVisibleChanged(EventArgs ^ e);
protected virtual void OnVisibleChanged(EventArgs e);
abstract member OnVisibleChanged : EventArgs -> unit
override this.OnVisibleChanged : EventArgs -> unit
Protected Overridable Sub OnVisibleChanged (e As EventArgs)
Parametri
Esempio
L'esempio di codice seguente è un metodo di generazione di eventi che viene eseguito quando viene modificato il valore della Text proprietà. La Control classe include diversi metodi con il modello On di nome PropertyNameChanged che genera l'evento PropertyName corrispondente quando il valore PropertyNameChanged viene modificato (PropertyName rappresenta il nome della proprietà corrispondente).
Nell'esempio di codice seguente viene modificato l'oggetto ForeColor di una TextBox classe derivata che visualizza i dati di valuta. Nell'esempio il testo viene convertito in un numero decimale e viene modificato in ForeColorColor.Red se il numero è negativo e in Color.Black se il numero è positivo. In questo esempio è necessario disporre di una classe che deriva dalla TextBox classe .
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
Commenti
La generazione di un evento richiama il gestore eventi tramite un delegato. Per altre informazioni, vedere Gestione e generazione di eventi.
Il OnVisibleChanged metodo consente inoltre alle classi derivate di gestire l'evento senza associare un delegato. Questa è la tecnica preferita per gestire l'evento in una classe derivata.
Note per gli eredi
Quando si esegue l'override OnVisibleChanged(EventArgs) in una classe derivata, assicurarsi di chiamare il metodo della OnVisibleChanged(EventArgs) classe di base in modo che i delegati registrati ricevano l'evento.