Control.OnContextMenuChanged 方法

引发 ContextMenuChanged 事件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Protected Overridable Sub OnContextMenuChanged ( _
    e As EventArgs _
)
用法
Dim e As EventArgs

Me.OnContextMenuChanged(e)
protected virtual void OnContextMenuChanged (
    EventArgs e
)
protected:
virtual void OnContextMenuChanged (
    EventArgs^ e
)
protected void OnContextMenuChanged (
    EventArgs e
)
protected function OnContextMenuChanged (
    e : EventArgs
)

参数

备注

引发事件时会通过委托调用事件处理程序。有关更多信息,请参见 引发事件

OnContextMenuChanged 方法还允许派生类对事件进行处理而不必附加委托。这是在派生类中处理事件的首选技术。

给继承者的说明 在派生类中重写 OnContextMenuChanged 时,一定要调用基类的 OnContextMenuChanged 方法,以便已注册的委托对事件进行接收。

示例

下面的代码示例是当 Text 属性值更改时执行的事件引发方法。Control 类有几种名称模式为 OnPropertyNameChanged 的方法,当 PropertyName 值(PropertyName 表示相应属性的名称)更改时,这些方法将引发相应的 PropertyNameChanged 事件。

下面的代码示例更改显示货币数据的 TextBox 派生类的 ForeColor。该示例将文本转换成十进制数,并且如果该数为负数,则将 ForeColor 更改为 Color.Red;如果该数为正数,则更改为 Color.Black。此示例要求您有从 TextBox 类派生的类。

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
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:
   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 void OnTextChanged(System.EventArgs e)
{
    try {
        // Convert the text to a Double and determine
        // if it is a negative number.
        if (System.Double.Parse(this.get_Text()) < 0) {
            // If the number is negative, display it in Red.
            this.set_ForeColor(Color.get_Red());
        }
        else {
            // If the number is not negative, display it in Black.
            this.set_ForeColor(Color.get_Black());
        }
    }
    catch (System.Exception exp) {
        // If there is an error, display the 
        // text using the system colors.
        this.set_ForeColor(SystemColors.get_ControlText());
    }
    super.OnTextChanged(e);
} //OnTextChanged

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
ContextMenuChanged
ContextMenu