Hey all. I find myself having some issues with parent and child communication.

What I have been asked to do is create a MDI program with many children.

I was able to get that part working great but then the kicker was brought in that the if the focus changes to a different child window that the status bar on the parent will update with the name of the currently focused child.

I was told to use this method in the child form but none of the code I place in here is ever reached when I test it in the debugger

private void Child_MdiChildActivate(object sender, EventArgs e)
{

}

this was in the parent form used to open a new child

private void openToolStripMenuItem_Click(object sender, EventArgs e)
       {
           count++;
           Child n = new Child();
           n.Text = "Window " + count.ToString();
           n.Name = "child" + count.ToString();
           n.MdiParent = this;
           n.MouseDown += new MouseEventHandler(Child_MouseDown);
           n.BackColor = this.b;
           n.ForeColor = this.f;
           n.Show();
           tssMessage.Text = n.Text;
           changeCountStatus();
       }

Sorry it seems like such a silly question but I have been searching my textbooks and online for hours in search of a solution.

Recommended Answers

All 5 Replies

To give more information: this the code in the child currently

parent oParent = Child as parent;

        protected virtual void mouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.Activate();
                
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            oParent = (parent)MdiParent;
            AddMouseDownHandler(this);
        }

        protected void AddMouseDownHandler(Control ctrl)
        {
            ctrl.MouseDown += new MouseEventHandler(mouseDown);
            for (int i = 0; i < ctrl.Controls.Count; i++)
            {
                AddMouseDownHandler(ctrl.Controls[i]);
            }
        }

        private void Child_Load(object sender, EventArgs e)
        {
            oParent.tssMessage.Text = "hi";
        }

        private void Child_MdiChildActivate(object sender, EventArgs e)
        {

            
             
            
        }

MdiChildActivate is an event called by the parent MDI when a child form is activated.
The event handler should be on the main form not the child form.
Try this.

private void MDIParent1_MdiChildActivate(object sender, EventArgs e)
{
    string childName = "No Child Open";
    if (this.ActiveMdiChild != null)
    {
        childName = this.ActiveMdiChild.Text;
    }
    // copy childName to status bar
}

thank you so much it is working with a slight adjustment

private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            count++;
            Child n = new Child();
            
          
            n.Text = "Window " + count.ToString();
            n.Name = "child" + count.ToString();
            n.MdiParent = this;
            n.MouseDown += new MouseEventHandler(Child_MouseDown);

//I had to add this line to the declaration of the child

            n.Activated += new EventHandler(ChildForm_Activated);
            n.BackColor = this.b;
            n.ForeColor = this.f;
            
            n.Show();
            tssMessage.Text = n.Text;
            changeCountStatus();
        }

//then I added this  method to make it work :)
        protected void ChildForm_Activated(object sender, System.EventArgs e)
        {
            string childName = "No Child Open";
            if (this.ActiveMdiChild != null)
            {
                childName = this.ActiveMdiChild.Text;
            }
            tssMessage.Text = childName;
       }

No. What you needed to do was attach to the MdiChildActivate event of the main form (perhaps in the load event or constuctor.)

public MDIParent1()

        {
            InitializeComponent();
            this.MdiChildActivate += new System.EventHandler(this.MDIParent1_MdiChildActivate);
        }

or

private void MDIParent1_Load(object sender, EventArgs e)
        {
            this.MdiChildActivate += new System.EventHandler(this.MDIParent1_MdiChildActivate);
        }

My way worked as well for what I needed but yours definitely looks like clear code. and I will remember it for next time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.