Add or remove MDI form discovery with child form

Is there an event that I can use to determine if a child form has been added or removed from an MDI parent?

+3
source share
8 answers

No no. You will need to subclass the form and expose specific events that would indicate when the child will be added, and then route all the attachments of the child forms using the method that binds the child form and also raises the event.

+2
source

Yes. In the main MDI form, connect to the MdiChildActivated event.

Same:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.MdiChildActivate += new EventHandler(Form1_MdiChildActivate);
        }

        void Form1_MdiChildActivate(object sender, EventArgs e)
        {
            MessageBox.Show("Activated");
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form2 = new Form2();
            form2.MdiParent = this;
            form2.Show();
        }
    }

And this event fires when the child form is activated or deactivated.

+6

MdiChildActivate . , FormClosed.

private List<Form> ChildFormList = new List<Form>();

private void MyForm_MdiChildActivate(object sender, EventArgs e)
{
    Form f = this.ActiveMdiChild;

    if (f == null)
    {
        //the last child form was just closed
        return;
    }

    if (!ChildFormList.Contains(f))
    {
        //a new child form was created
        ChildFormList.Add(f);
        f.FormClosed += new FormClosedEventHandler(ChildFormClosed);
    }
    else
    {
        //activated existing form
    }
}

private void ChildFormClosed(object sender, FormClosedEventArgs e)
{
    //a child form was closed
    Form f = (Form)sender;
    ChildFormList.Remove(f);
}
+4

, , , , MdiChildren .net 4. , , , , , .

    private void frmContainer_MdiChildActivate(object sender, EventArgs e)
    {
        tabWindows.RefreshLayout(this.MdiChildren.ToList());
    }

    public void RefreshLayout(List<Form> forms)
    {
        this.nextButtonLeft = 1;
        panel1.Controls.Clear();
        foreach (Form frm in forms)
        {
            if (!(frm.Disposing || frm.IsDisposed)) { addButton(frm); }
        }
    }
+1

, , MDI.

ParentForm MdiContainer, IsMdiContainer true, ParentForm.ControlAdded, MdiClient . MDI- MDI ControlAdded MdiClient, ,

  public ParentForm()
  {
    InitializeComponent();
    this.ControlAdded += Form1_ControlAdded;
    this.IsMdiContainer = true;

MdiClient.ControlAdded, ,

    void Form1_ControlAdded(object sender, ControlEventArgs e)
      {
           if(e.Control is MdiClient)
                e.Control.ControlAdded += MdiClient_ControlAdded;

      }

MDI Child MdiClient . , ChildForm.MdiParent , ControlAdded MdiClient .

void MdiClient_ControlAdded(object sender, ControlEventArgs e)
{

}

, , , MDI MDI. ControlRemoved MdiClient, , MDI.

, .

+1
private void closeToolStripMenuItem1_Click(object sender, EventArgs e)
    {
      List<Form> fm = this.MdiChildren.ToList();
        if(fm!=null && fm.Count>0)
        {
            foreach(Form lfm in fm)
            {
                lfm.Close();
            }
        }
    }
0

MdiChildActivate , MDI , , MDI, .

, ParentChanged MDI, .

public class MdiParentForm : Form
{
    // ...

    private Form CreateMdiChildForm()
    {
        var form = new MdiChildForm
        form.ParentChanged += MdiFormParentChangedHandler;
        form.MdiParent = this;
        return form;
    }

    private void MdiFormParentChangedHandler(object sender, EventArgs args)
    {
        var form = sender as Form;
        if (form != null)
        {
            if (form.MdiParent != null)
            {
                // MDI child form will activate
                // ... your code here
            }
            else
            {
                // MDI child form will close
                form.ParentChanged -= MdiFormParentChangedHandler;
                // ... your code here
            }
        }
    }

    // ...
}
0

Recently, I wanted to determine when NO MDIchildren opened, so I could only display a panel with many “what to do” buttons when there were no child forms. (just exploring the design idea).

My final solution was exquisitely simple - add a timer to the parent form and start the timer every time the MdiChildActivate event determines that 1 child form is open.

    private void MyForm_MdiChildActivate(object sender, EventArgs e)
    {
        this.panel_Tools.Visible = false;
        if (this.MdiChildren.Count() == 1)
        {
            this.timer_WindowsCounter.Start();
        }
        else
        {
            this.timer_WindowsCounter.Stop();
        }

    }


    private void timer_WindowsCounter_Tick(object sender, EventArgs e)
    {
        if (this.MdiChildren.Count() == 0)
        {
            this.panel_Tools.Visible = true;
            this.timer_WindowsCounter.Stop();
        }
    }
-2
source

Source: https://habr.com/ru/post/1703050/


All Articles