views:

19

answers:

2

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?

+1  A: 
Tim Robinson
Doesn't work for TreeView (no support for Transparent backcolor)
Sphynx
+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