Search code examples
c#.netwinformsmdimdichild

Problem with controls in MDI form


I have placed a button on MDI form , now when I open a child form , the button remains on top and distracts the child form , is there a way to solve it? I have done following to send the button back when any child is activated.But I am wondering to call button1.BringToFront();

private void MDIParent1_MdiChildActivate(object sender, EventArgs e)
{
    button1.SendToBack();
}

I tried following but it doesnt work.

private void MDIParent1_Enter(object sender, EventArgs e)
{
    button1.BringToFront();
}

I think its a lasting bug of .net , there are many posts trying to solve it , using dock with a panel etc , but docking just for a button makes the application work space less.


Solution

  • I found the answer to my question ! here is how I implemented it , the post here was what I was missing.

    public MDIParent1()
            {
                InitializeComponent();
                foreach (var ctl in this.Controls)
                {
                    if (ctl is MdiClient)
                    {
                        (ctl as MdiClient).GotFocus  += Client_gotfocus;
                        (ctl as MdiClient).LostFocus  += Client_lostfocus;
                        break;
                    }
                }
    
            }
            private void Client_gotfocus(object sender, EventArgs e)
            {
                button1.BringToFront();
            }
            private void Client_lostfocus(object sender, EventArgs e)
            {
                button1.SendToBack ();
            }