我在 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/