Control.OnLocationChanged(EventArgs) 方法

定义

引发 LocationChanged 事件。

C#
protected virtual void OnLocationChanged(EventArgs e);

参数

e
EventArgs

包含事件数据的 EventArgs

示例

下面的代码示例是 Text 属性值更改时执行的事件引发方法。 ControlPropertyName 值发生更改时,PropertyName 值更改时,OnPropertyNameChanged 具有名称模式的多个方法(PropertyName 表示相应属性的名称)引发 相应的 propertyNameChanged 事件。

下面的代码示例更改显示货币数据的 TextBox 派生类的 ForeColor。 该示例将文本转换为小数,并将数字为负数时将 ForeColor 更改为 Color.Red;如果数字为正数,则将其更改为 Color.Black。 此示例要求你有一个派生自 TextBox 类的类。

C#
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);
}

注解

引发事件会通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnLocationChanged 方法还允许派生类在不附加委托的情况下处理事件。 这是处理派生类中的事件的首选技术。

继承者说明

在派生类中重写 OnLocationChanged(EventArgs) 时,请务必调用基类的 OnLocationChanged(EventArgs) 方法,以便注册的委托接收事件。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅