Reputation: 8651
I have my own control derived from Windows.Forms.Control and I am checking the Parent.BackColor inside the overrided of OnHandleCreated() method. At desing-time the Parent property first returns null, then - after form is completely loaded - returns the real parent window: the form itself.
We need to draw part of the custom control with the same color of the parent form background: when can we rely on the Control.Parent value?
Thanks.
Upvotes: 2
Views: 1560
Reputation: 9575
Actually I do not see problem here. Because as I understand you need parent for drawing something with parent back color, and when you receive your control's Paint event (OnPaint, WM_PAINT) parent already initialized.
Upvotes: 0
Reputation: 185643
You can use the ParentChanged
event to detect when that property changes and trigger a redraw (though one should occur automatically). There are some properties, however (BackColor
and ForeColor
being two of them, I believe) that are "inherited" from the parent if not set explicitly, so you should be able to use those as well.
Upvotes: 2
Reputation: 14483
you can use if( this.DesignMode ) return;
to determine if your control is in design and not get exception or to do additional checking. In Windows Forms, after InitilizeComponents, afaik Control always have parent.
Upvotes: 0