Control.OnTextChanged(EventArgs) Metoda

Definice

TextChanged Vyvolá událost.

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

Parametry

e
EventArgs

Objekt EventArgs , který obsahuje data události.

Příklady

Následující příklad kódu změní ForeColor odvozenou TextBox třídu zobrazující data měny. Příklad převede text na desetinné číslo a změní ForeColor hodnotu na Color.Red , pokud je číslo záporné, a na Color.Black hodnotu, pokud je číslo kladné. Tento příklad vyžaduje, abyste měli třídu, která je odvozena od TextBox třídy.

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

Poznámky

Vyvolání události spustí obslužnou rutinu události prostřednictvím delegáta. Další informace najdete v tématu Zpracování a vyvolávání událostí.

Metoda OnTextChanged také umožňuje odvozeným třídám zpracovat událost bez připojení delegáta. Toto je upřednostňovaná technika pro zpracování události v odvozené třídě.

Poznámky pro dědice

Při přepsání OnTextChanged(EventArgs) v odvozené třídě nezapomeňte volat metodu základní třídy OnTextChanged(EventArgs) , aby registrovaní delegáti obdrželi událost.

Platí pro

Viz také