Sdílet prostřednictvím


Control.OnLocationChanged(EventArgs) Metoda

Definice

LocationChanged Vyvolá událost.

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

Parametry

e
EventArgs

Obsahuje EventArgs data události.

Příklady

Následující příklad kódu je metoda vyvolání událostí, která se provede při Text změně hodnoty vlastnosti. Třída Control má několik metod se vzorem name pattern OnPropertyNameChanged , které vyvolává odpovídající PropertyNameChanged událost, když PropertyName hodnota změní (PropertyName představuje název odpovídající vlastnosti).

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í ForeColorColor.Red hodnotu, zda je číslo záporné a jestli Color.Black je číslo kladné. Tento příklad vyžaduje, abyste měli třídu odvozenou 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 vyvolá obslužnou rutinu události prostřednictvím delegáta. Další informace naleznete v tématu Zpracování a vyvolávání událostí.

Metoda OnLocationChanged také umožňuje odvozené třídy 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í OnLocationChanged(EventArgs) v odvozené třídě nezapomeňte volat metodu základní třídy OnLocationChanged(EventArgs) , aby zaregistrovaní delegáti obdrželi událost.

Platí pro

Viz také