Question

Lets say there are two overlapping controls:

  • Control A
  • Control B

I want to keep Control A over Control B in all situation.

The z-order of Control B can change dynamically either by calling BringToFront() or setting its index in parent by calling Parent.SetChildIndex(ControlB, 0). So whenever this happens, I want Control A to come in front of ControlB.

I was looking for some event that occurs when z-order is changed, so that I could bring Control A to front. Is there any such event? or there is some way to do this?

Était-ce utile?

La solution

Not a fan of spamming things on Paint Events. Especially since WM_PAINT wont be sent when the control is off screen, so actions may not occur at the times your code expects them to.

A much more controlled way to do this is handle when the Controls position changes.

Change your control to derive from this (change base class to whatever you use) and set the field:

firstControl.ChildControl = secondControl;

public class AlwaysParentingControl : TextBox
{
    private const Int32 WM_WINDOWPOSCHANGED = 0x47;
    public Control ChildControl { get; set; }

    protected override void WndProc(ref Message m)
    {
        if( m.Msg == WM_WINDOWPOSCHANGED && ChildControl != null)
            ChildControl.BringToFront();

        base.WndProc(ref m);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top