Button.Parent 在构造函数中调用时为 Null

Button.Parent is Null, when Called in the Constructor

我正在使用 Windows 表单和 .NET Framework v3.5。

我继承了System.Windows.Forms.Buttonclass,
而在继承者中,我在Ctor中写了一些代码。

在该代码中,我需要调用 this.Parent
然而 this 存在,this.Parent returns 在 Button 的 Ctor 中调用时为 null。

由于控件没有像窗体那样的Load event
我将需要另一个解决方案,使我能够在创建按钮(并添加到其父窗体)后获得按钮的父级

我可以用什么来实现这个目标?

您可以订阅自己的ParentChanged event (or, better yet, override the OnParentChanged方法,这样您就不必设置事件订阅了)