locked
Bring a child form to fornt RRS feed

  • Question

  • Hi, all,

    Please refer the code bellow:
            private void Form1_Load(object sender, EventArgs e)  
            {  
                label1.Text = "AAAA";  
                Form2 f = new Form2();  
                f.MdiParent = this;  
                f.BringToFront();  
                f.Show();  
              
                f.Activate();  
            }  
     
    But the form2 f is always in the back of label1. How to bring the form to front?

    thanks


    Andrew Huang andrew.huang@ymail.com
    Thursday, October 16, 2008 9:11 PM

Answers

  • Controls placed on the surface of an MdiForm show up in front of child forms by default.  Simply don't place the label on an MDI Form's background.  If you need text there, override the OnShown method, and draw a string on the BackgroundImage image property:

        public partial class MdiFormInFrontIssue : Form
        {  
            public MdiFormInFrontIssue()  
            {  
                InitializeComponent();  
            }  
     
            protected override void OnShown(EventArgs e)  
            {           
                base.OnShown(e);  
                this.BackgroundImage = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);  
                Graphics.FromImage(this.BackgroundImage).DrawString("hello"new Font("Arial", 12f), new SolidBrush(Color.Black), new PointF(10f, 10f));  
                Form2 form = new Form2();  
                form.MdiParent = this;  
                form.Show();  
            }  
        } 

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Tuesday, October 21, 2008 3:58 AM
    Friday, October 17, 2008 8:07 PM
  • As I said before, the MDI container form is not a place to be putting user controls.  You need to put those controls on the child forms.  As much as you're wanting to put user controls on the MDI form container, that defeats the purpose of the MDI Container, which is designed to contain forms, not user controls. 
    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Tuesday, October 21, 2008 3:58 AM
    Friday, October 17, 2008 8:45 PM

All replies

  • f.TopMost = true;

    You don't need to Activate form.


    AlexB
    Thursday, October 16, 2008 9:28 PM
  • Thanks, Alex.

    It doesn't work.

                label1.Text = "AAAA";  
                Form2 f = new Form2();  
                f.MdiParent = this;  
                f.TopMost = true;  
                f.BringToFront();  
                f.Show();  
                f.Activate();  
     

    Andrew Huang andrew.huang@ymail.com
    Friday, October 17, 2008 7:42 PM
  • Controls placed on the surface of an MdiForm show up in front of child forms by default.  Simply don't place the label on an MDI Form's background.  If you need text there, override the OnShown method, and draw a string on the BackgroundImage image property:

        public partial class MdiFormInFrontIssue : Form
        {  
            public MdiFormInFrontIssue()  
            {  
                InitializeComponent();  
            }  
     
            protected override void OnShown(EventArgs e)  
            {           
                base.OnShown(e);  
                this.BackgroundImage = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);  
                Graphics.FromImage(this.BackgroundImage).DrawString("hello"new Font("Arial", 12f), new SolidBrush(Color.Black), new PointF(10f, 10f));  
                Form2 form = new Form2();  
                form.MdiParent = this;  
                form.Show();  
            }  
        } 

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Tuesday, October 21, 2008 3:58 AM
    Friday, October 17, 2008 8:07 PM
  • David M Morton said:

    Controls placed on the surface of an MdiForm show up in front of child forms by default.  Simply don't place the label on an MDI Form's background.  If you need text there, override the OnShown method, and draw a string on the BackgroundImage image property:

        public partial class MdiFormInFrontIssue : Form
        {  
            public MdiFormInFrontIssue()  
            {  
                InitializeComponent();  
            }  
     
            protected override void OnShown(EventArgs e)  
            {           
                base.OnShown(e);  
                this.BackgroundImage = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);  
                Graphics.FromImage(this.BackgroundImage).DrawString("hello"new Font("Arial", 12f), new SolidBrush(Color.Black), new PointF(10f, 10f));  
                Form2 form = new Form2();  
                form.MdiParent = this;  
                form.Show();  
            }  
        } 

    David Morton - http://blog.davemorton.net/



    Thanks, David.
    In fact, there are huge controls (including some user controls) in the parent form and put them in in design time. How can I do in this situation?

    thanks.
    Andrew Huang andrew.huang@ymail.com
    Friday, October 17, 2008 8:24 PM
  • As I said before, the MDI container form is not a place to be putting user controls.  You need to put those controls on the child forms.  As much as you're wanting to put user controls on the MDI form container, that defeats the purpose of the MDI Container, which is designed to contain forms, not user controls. 
    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Tuesday, October 21, 2008 3:58 AM
    Friday, October 17, 2008 8:45 PM
  • Try this:

    label1.SendToBack();
    label1.SendToBack();
    label1.SendToBack();
    f.BringToFront();
    f.BringToFront();
    f.BringToFront();


    Viral.
    Saturday, October 18, 2008 5:36 AM
  • Try this:

    label1.SendToBack();
    label1.SendToBack();
    label1.SendToBack();
    f.BringToFront();
    f.BringToFront();
    f.BringToFront();


    Viral.

    LOL. This reminds me of Mufasa's glitch on "Austin Powers: International Man of Mystery" - If you say it 3 times, the compiler will give in.
    Avoid Sears Home Improvement
    Monday, September 14, 2009 3:18 PM
  • ==========
    LOL. This reminds me of Mufasa's glitch on "Austin Powers: International Man of Mystery" - If you say it 3 times, the compiler will give in.
    ===========

    Not really, do you see any other way to set Z-Order in .NET 2.0 WinForms? If you do not, please think twice before posting.
    Wednesday, September 16, 2009 9:40 AM