简体   繁体   中英

label in MDI parent form do not remain in background of child form?

Please guide and help me.

I have a MDI parent form which has a label at its center (to display application name in center). On opening a form in this MDI parent, this label should appear on back side of newly opened form, but on showing a child form, label appears in front of newly opened form (appears like newly opened form is between label and MDI parent).

How to manage it please guide me.

thanks

This will hide the label while you have active MDI Children en show it again once there is no active child anymore.

    private void Form1_MdiChildActivate(object sender, EventArgs e)
    {
        if (ActiveMdiChild != null)
            label1.SendToBack();
        else
            label1.BringToFront();
    }

I hope this helps.

public partial class MyMdiForm : Form
{
    public MyMdiForm()
    {
        InitializeComponent();
        foreach (Control control in Controls)
        {
            if (control is MdiClient)
                control.Paint += mdiBackgroundPaint;
        }
    }

    private void mdiBackgroundPaint(object sender, PaintEventArgs e)
    {
        var mdi = sender as MdiClient;
        if (mdi == null) return;

        e.Graphics.Clip = new System.Drawing.Region(mdi.ClientRectangle);
        e.Graphics.DrawString("*** YOUR NAME HERE ***",this.Font,Brushes.Red,100F,100F);
    }
}

The problem is that your label is not added to the MdiClient (ie the grey Mdi container) but to the form.

But unfortunately, AFAIK, it's not possible to add controls to an MdiClient.

The only way is drawing what you want on the Paint event of the MdiClient, as suggested in this article:

http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/MDI_Client_Area_Painting/article.asp

Well, apparently something is wrong with the Z-order of your MDI parent window's child controls. (The parent's child controls include your label as well as any MDI document windows.)

You haven't shown any of your code, so I can't post a specific solution. Perhaps look into the SendToBack method : You could try calling:

yourBackgroundLabel.SendToBack();

when you open a new MDI (child/document) window.

Well, I did one trick and it works for me. We usually write an application name in the center and expect it to show to the user. And many people here said that MdiParent is only for Forms and not for other tools like we cannot hide label/panel behind MdiChild form.

So what I did is I wrote all the things like application name, contact, email etc etc etc in a new Form say frmMdiBody , Set its formBorderStyle = None and set the desired length of the form, StartPosition = CenterScreen and in Timer.Tick , I wrote the following : (Didn't work for me in Load event)

  Dim NewMDIChild As frmMdiBody = MdiChildren.OfType(Of frmMdiBody)().SingleOrDefault
  If NewMDIChild Is Nothing Then
      NewMDIChild = New frmMdiBody
      NewMDIChild.MdiParent = Me
      NewMDIChild.Show()
  End If

This above code also checks if there is one form open so that it won't open many frmMdiBody again and again as we are writing in Timer.Tick event

Someone can rectify me if I am wrong. I'd do the changes too if seems appealing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM