Control.OnContextMenuChanged(EventArgs) Metoda

Definicja

ContextMenuChanged Zgłasza zdarzenie.

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

Parametry

e
EventArgs

Element EventArgs zawierający dane zdarzenia.

Przykłady

Poniższy przykład kodu to metoda generowania zdarzeń wykonywana po Text zmianie wartości właściwości. Klasa Control ma kilka metod o nazwie OnPropertyNameChanged, które zgłaszają odpowiednie zdarzenie PropertyName, gdy wartość PropertyNameChanged zmienia się (PropertyName reprezentuje nazwę odpowiedniej właściwości).

Poniższy przykład kodu zmienia ForeColor klasę pochodną TextBox wyświetlającą dane waluty. Przykład konwertuje tekst na liczbę dziesiętną i zmienia ForeColorColor.Red wartość na wartość , jeśli liczba jest ujemna i jeśli Color.Black liczba jest dodatnia. W tym przykładzie jest wymagana klasa pochodząca z TextBox klasy .

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

Uwagi

Podnoszenie zdarzenia wywołuje program obsługi zdarzeń przez delegata. Aby uzyskać więcej informacji, zobacz Obsługa i podnoszenie zdarzeń.

Metoda OnContextMenuChanged umożliwia również klasom pochodnym obsługę zdarzenia bez dołączania delegata. Jest to preferowana technika obsługi zdarzenia w klasie pochodnej.

Uwagi dotyczące dziedziczenia

Podczas zastępowania OnContextMenuChanged(EventArgs) w klasie pochodnej należy wywołać metodę klasy OnContextMenuChanged(EventArgs) bazowej, aby zarejestrowani delegaci otrzymywali zdarzenie.

Dotyczy

Zobacz też