Control.OnParentVisibleChanged(EventArgs) Control.OnParentVisibleChanged(EventArgs) Control.OnParentVisibleChanged(EventArgs) Control.OnParentVisibleChanged(EventArgs) Method

定义

当控件容器的 VisibleChanged 属性值更改时,将引发 Visible 事件。Raises the VisibleChanged event when the Visible property value of the control's container changes.

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

参数

e
EventArgs EventArgs EventArgs EventArgs

包含事件数据的 EventArgsAn EventArgs that contains the event data.

示例

下面的代码示例是一个事件引发方法,该方法在Text属性值更改时执行。The following code example is an event-raising method that is executed when the Text property value changes. Changed On``Changed 类具有多个方法,这些方法具有 name 模式 PropertyName,在 PropertyName 值更改时引发相应的 PropertyName 事件( Control PropertyName表示相应属性的名称。The Control class has several methods with the name pattern OnPropertyNameChanged that raise the corresponding PropertyNameChanged event when the PropertyName value changes (PropertyName represents the name of the corresponding property).

下面的代码示例更改ForeColor TextBox了显示货币数据的派生类的。The following code example changes the ForeColor of a TextBox derived class displaying currency data. 该示例将文本转换为十进制数字,如果ForeColor Color.Black数字为负Color.Red ,则将更改为,如果数字为正,则更改为。The example converts the text to a decimal number and changes the ForeColor to Color.Red if the number is negative and to Color.Black if the number is positive. 此示例要求具有派生自TextBox类的类。This example requires that you have a class that derives from the TextBox class.

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

注解

引发事件时,将通过委托调用事件处理程序。Raising an event invokes the event handler through a delegate. 有关详细信息, 请参阅处理和引发事件For more information, see Handling and Raising Events.

OnParentVisibleChanged方法还使派生类可以处理事件,而无需附加委托。The OnParentVisibleChanged method also enables derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

继承者说明

在派生类中重写 OnParentVisibleChanged(EventArgs) 时,一定要调用基类的 OnParentVisibleChanged(EventArgs) 方法,以便已注册的委托对事件进行接收。When overriding OnParentVisibleChanged(EventArgs) in a derived class, be sure to call the base class's OnParentVisibleChanged(EventArgs) method so that registered delegates receive the event.

适用于

另请参阅