I have a WinForms control inherited from TreeView and I want it to automatically adjust background color according to the form (also customized) background. How to make it?
Doesn't work for TreeView (no support for Transparent backcolor)
Sphynx
2010-08-01 11:07:09
+1
A:
Making a control aware of its parent is normally a bad idea. There however is a dedicated method to detect the parent BackColor changing, so it's okay:
using System;
using System.Windows.Forms;
class MyTreeView : TreeView {
protected override void OnParentChanged(EventArgs e) {
if (this.Parent != null) this.BackColor = this.Parent.BackColor;
base.OnParentChanged(e);
}
protected override void OnParentBackColorChanged(EventArgs e) {
this.BackColor = this.Parent.BackColor;
base.OnParentBackColorChanged(e);
}
}
Hans Passant
2010-08-01 12:54:29