Control.OnParentBackgroundImageChanged(EventArgs) Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
BackgroundImageChanged Вызывает событие при BackgroundImage изменении значения свойства контейнера элемента управления.
protected:
virtual void OnParentBackgroundImageChanged(EventArgs ^ e);
protected virtual void OnParentBackgroundImageChanged(EventArgs e);
abstract member OnParentBackgroundImageChanged : EventArgs -> unit
override this.OnParentBackgroundImageChanged : EventArgs -> unit
Protected Overridable Sub OnParentBackgroundImageChanged (e As EventArgs)
Параметры
Примеры
В следующем примере кода используется метод создания событий, который выполняется при Text изменении значения свойства. Класс Control имеет несколько методов с шаблоном Onname PropertyNameChanged , которые вызывают соответствующее событие PropertyNameChanged при изменении значения PropertyName (PropertyName представляет имя соответствующего свойства).
В следующем примере кода изменяется ForeColor производный TextBox класс, отображающий данные валюты. В примере текст преобразуется в десятичное число и изменяется ForeColorColor.Red значение, если число отрицательное, а Color.Black если число положительное. В этом примере требуется, чтобы у вас был класс, производный от TextBox класса.
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
Комментарии
При вызове события обработчик событий вызывается через делегат. Дополнительные сведения см. в разделе "Обработка и создание событий".
Метод OnParentBackgroundImageChanged также позволяет производным классам обрабатывать событие без присоединения делегата. Это предпочтительный способ обработки события в производном классе.
Примечания для тех, кто наследует этот метод
При переопределении OnParentBackgroundImageChanged(EventArgs) в производном классе обязательно вызовите метод базового класса OnParentBackgroundImageChanged(EventArgs) , чтобы зарегистрированные делегаты получали событие.