Поделиться через


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 имеет несколько методов с шаблоном 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

Комментарии

При вызове события обработчик событий вызывается через делегат. Дополнительные сведения см. в разделе "Обработка и создание событий".

Метод OnContextMenuChanged также позволяет производным классам обрабатывать событие без присоединения делегата. Это предпочтительный способ обработки события в производном классе.

Примечания для тех, кто наследует этот метод

При переопределении OnContextMenuChanged(EventArgs) в производном классе обязательно вызовите метод базового класса OnContextMenuChanged(EventArgs) , чтобы зарегистрированные делегаты получали событие.

Применяется к

См. также раздел