Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 834875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:44:19+00:00 2026-05-15T04:44:19+00:00

I have a control in my parent mdi form which I wish to be

  • 0

I have a control in my parent mdi form which I wish to be given the focus if all mdi children have closed. I’ve tried hooking into the child form’s FormClosed event and setting the focus from there but when I test it, my control is not left with focus when I close the mdi child.

Can anyone tell me what I’m missing?

In the sample below, “first focus” gets hit and does its job correctly (if I comment out the first focus line, my tree does not get focus on startup, so it must be doing its job, right?)
Unfortunately, even though “second focus” gets hit, my tree does not end up with the focus when I close the child window.

Has focus on startup.

Has focus http://www.programmingforpeople.com/images/stackoverflow/focus1.PNG

Not focussed after closing child window.

No focus http://www.programmingforpeople.com/images/stackoverflow/focus2.PNG


Sample

using System;
using System.Windows.Forms;

namespace mdiFocus
{
   class ParentForm : Form
   {
      public ParentForm()
      {
         IsMdiContainer = true;
         tree = new TreeView();
         tree.Nodes.Add("SomeNode");
         tree.Dock = DockStyle.Left;
         Controls.Add(tree);
      }
      protected override void OnShown(EventArgs e)
      {
         Form child = new Form();
         child.MdiParent = this;
         child.Show();
         child.FormClosed += new FormClosedEventHandler(child_FormClosed);
         tree.Focus(); // first focus works ok
      }
      void child_FormClosed(object sender, FormClosedEventArgs e)
      {
         tree.Focus(); // second focus doesn't seem to work, even though it is hit :(
      }
      TreeView tree;
   }

   static class Program
   {
      [STAThread]
      static void Main()
      {
         Application.Run(new ParentForm());
      }
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:44:20+00:00Added an answer on May 15, 2026 at 4:44 am

    I repro, smells like the WF logic that hunts for a focusable item is running after the event and messes up the focus. These kind of problems can be elegantly solved by running code after the event is processed by using Control.BeginInvoke(). This worked well:

        private void toolStripButton1_Click(object sender, EventArgs e) {
            Form child = new Form2();
            child.MdiParent = this;
            child.FormClosed += new FormClosedEventHandler(child_FormClosed);
            child.Show();
        }
    
        void child_FormClosed(object sender, FormClosedEventArgs e) {
            if (this.MdiChildren.Length == 1) {
                this.BeginInvoke(new MethodInvoker(() => treeView1.Focus()));
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which adds a control to a parent control which is
I have a MDI window with a datagridview control which is used to display
I have an iframe which will be inserted dynamically into the parent page (
I am working in Desktop application in C#. I have an MDI Parent form
I have a MDI parent form. When user presses Enter I want the Application
I have a TextBox control in a form which is still pulling in data
I have a parent control (main form) and a child control (user control). The
I have an ASP.NET 2.0 ListView control (aka:parent) and configured inside this ListView I
In WinForms, you have Control.BeginInvoke() , which means you can marshall a call from
I have a control ...any System.Windows.Forms.Control . say for eg. label. I wish to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.