共用方式為


Control.OnContextMenuChanged(EventArgs) 方法

定義

警告

ContextMenu is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use ContextMenuStrip instead.

引發 ContextMenuChanged 事件。

protected:
 virtual void OnContextMenuChanged(EventArgs ^ e);
protected virtual void OnContextMenuChanged(EventArgs e);
[System.ComponentModel.Browsable(false)]
[System.Obsolete("`ContextMenu` is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use `ContextMenuStrip` instead.", false, DiagnosticId="WFDEV006", UrlFormat="https://aka.ms/winforms-warnings/{0}")]
protected virtual void OnContextMenuChanged(EventArgs e);
abstract member OnContextMenuChanged : EventArgs -> unit
override this.OnContextMenuChanged : EventArgs -> unit
[<System.ComponentModel.Browsable(false)>]
[<System.Obsolete("`ContextMenu` is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use `ContextMenuStrip` instead.", false, DiagnosticId="WFDEV006", UrlFormat="https://aka.ms/winforms-warnings/{0}")>]
abstract member OnContextMenuChanged : EventArgs -> unit
override this.OnContextMenuChanged : EventArgs -> unit
Protected Overridable Sub OnContextMenuChanged (e As EventArgs)

參數

e
EventArgs

一個 EventArgs 包含事件資料的 。

屬性

範例

以下程式碼範例是一個事件引發方法,當屬性值改變時 Text 會執行。 該Control類別有多個名為 PropertyNameChanged 的命名模式On方法,當 PropertyName 值改變時會觸發相應的 PropertyNameChanged 事件(PropertyName 代表對應屬性的名稱)。

以下程式碼範例變更ForeColorTextBox了顯示貨幣資料的衍生類別。 範例將文字轉換為十進位數字,若數字為負數則將 轉換為 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

備註

發起事件會透過代理呼叫事件處理者。 欲了解更多資訊,請參閱 處理與提升事件

OnContextMenuChanged 方法也允許衍生類別在不附加代理的情況下處理事件。 這是在衍生類別中處理事件的首選技術。

給繼承者的注意事項

在導出類別中覆寫 OnContextMenuChanged(EventArgs) 時,務必呼叫基底類別的方法 OnContextMenuChanged(EventArgs) ,讓註冊代理接收事件。

適用於

另請參閱