c# - Control.ParentChanged 事件何时发生?

标签 c# winforms

我在 MSDN 上读到了 Control.ParentChanged 事件 http://msdn.microsoft.com/en-us/library/system.windows.forms.control.parentchanged(VS.71).aspx

但我不明白示例代码:源代码中根本没有出现 ParentChanged ?

private void currencyTextBox_TextChanged(object sender, EventArgs e)
{
   try
   {
      // Convert the text to a Double and determine if it is a negative number.
      if(double.Parse(currencyTextBox.Text) < 0)
      {
         // If the number is negative, display it in Red.
         currencyTextBox.ForeColor = Color.Red;
      }
      else
      {
         // If the number is not negative, display it in Black.
         currencyTextBox.ForeColor = Color.Black;
      }
   }
   catch
   {
      // If there is an error, display the text using the system colors.
      currencyTextBox.ForeColor = SystemColors.ControlText;
   }
}

所以我不明白 Control.ParentChanged Event 是什么或做什么。

最佳答案

呵呵,他们就是想不出一个好的例子。并通过显示通用的 FooChanged 事件处理程序来解决问题。是的,没用。

自己实现 ParentChanged 事件处理程序是很不寻常的。这在 Winforms 内部很重要,BackColor、ForeColor、Font 等属性是“环境”属性。如果它们没有被默认值覆盖,那么它们将获得 Parent 的值。这当然意味着注意到父对象发生了变化真的很重要。 winforms 代码已经处理了它,你很少需要担心它。当然,除非您创建自己的环境属性。

关于c# - Control.ParentChanged 事件何时发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4336046/

相关文章:

c# - 与重复的组合 C#

c# - 如何在 DataGrid WPF 中显示 SortedDictionary?

c# - 从 WebApi 调用 STA COM 对象

c# - .dll 引用与。项目引用

c# - C# 返回问题

c# - e.Data.GetDataPresent 在 WinForms 拖放处理程序中不起作用?

c# - 尝试执行 SQL 查询时出现无效对象错误

C# 在开始时隐藏新窗体

c# - 注册表中的字体链接

c# - 在 ListView 中更改列