title: Problems adding a non-modal, child Windows.Forms.Form to a main Form which prevent child from receiving focus
body: <p>Preface (likely to be removed later, after answers posted).  This is a question to which I could not find an answer anywhere online (after a couple of days searching).  I found an answer myself through trial-ans-error, but I am not particularly familiar the subject matter - forms - and so my solution may not be the best, or even correct.</p>
  
  <p>I have created a small application using Windows Forms.  The application does some (lengthy) data processing, and it needs a second, separate window to display (ongoing) errors.  For this, I am using a (simple) non-modal form.  </p>
  
  <p>The structure is fairly simple:</p>
  
  <pre><code>public static class Program
  {
      [STAThread]
      public static int Main(string[] args)
      {
          MainForm mainForm = new MainForm();
          mainForm.showTheForm();
      }
  }
  
  
  public partial class MainForm : Form
  {
      public MainForm()
      {
          InitializeComponent();
          init();
      }
  
      private NonModalInfo m_infoBox = null;      
      //plus a few other data members...
  
      private void init()
      {
          //initialize some data members and other stuff, etc.
  
          //Then, this critical line:
          m_infoBox = new NonModalInfo();
      }
  
      public void showTheForm()
      {
          this.ShowDialog();
      }
  
      //private void somecontrol_click(...)
      //{
          //for(some loop)
          //{
          //    doStuff;
          //    showInfo("SomeMessage");
          //    doStuff;
          //}
      //}
  
      //this method only gets called while the mainForm is showing, 
      //  as, for example, in the somecontrol_click method above
      public void showInfo(string msg)
      {
          if (m_infoBox == null || m_infoBox.IsDisposed)
          {
              m_infoBox = new NonModalInfo();
          }
          m_infoBox.updateMessage(msg);
          if (this.Visible)
          {
              if(!m_infoBox.Visible)
              {
                  m_infoBox.Owner = this;
                  m_infoBox.Show();
              }
          }
      }
  }
  
  public partial class NonModalInfo : Form
  {
      public NonModalInfo()
      {
          InitializeComponent();
      }
      //This form has one element, a label called lb_Message.
      public void updateMessage(string msg)
      {
          if (msg != null)
          {
              lb_Message.Text = msg;
              if (Visible)
              {
                  Update();
              }
          }
      }
  }
  </code></pre>
  
  <p>The basic idea here is that the <code>NonModalInfo</code> form is used by the <code>mainForm</code> to display logging and/or error information to the user.  The structure above, as I have it, works perfectly.  The issue is the line I have commented above as <code>Then, this critical line:</code>.  Without that line - which is how I wrote the code originally - the code almost works: the non-modal form is opened, displayed, updates, etc.  The issue, is that the non-modal form can never receive focus: any attempt to click on the non-modal form to give it focus results in the <code>mainForm</code> blinking and maintaining focus.  The behaviour is exactly what one would expect when the <code>mainForm</code> is opened modally, so that the non-modal form cannot receive focus.</p>
  
  <p>Now, the <code>mainForm</code> <em>is</em> being opened modal (<code>ShowDialog()</code>), so perhaps this is not unexpected, but all the documentation and examples on the web indicate that a (modal) main dialog such as this should be able to open a non-modal child dialog, and that the child dialog should be able to recieve focus.  Furthermore, simply ensuring that the member data element <code>m_infoBox</code> is initialized with some instance of <code>NonModalInfo</code>, then the behaviour is different: the child form will be given focus, no problem.  From appearances, in the case where <code>m_infoBox</code> is initialized with a non-null instance before showing the <code>mainForm</code>, the <code>mainForm</code> seems as if it is shown as non-modal (in spite of having been shown with <code>ShowDialog()</code>).</p>
  
  <p>My question is, why do I need to ensure the <code>mainForm</code>'s member <code>m_infoBox</code> is initialized with a non-null instance?  Is there any way to construct the <code>mainForm</code> and "show" it, so that when I do construct, add and show the non-modal child, the child can receive focus?  Where is there documentation on this?  Is the code above actually the correct way to do this?  Can anyone explain what is going on under the hood?</p>
  
  <p>Edit:
  In fact, the situation is more complicated than this.  If the child non-modal form is visible at the time the parent is shown, the parent will be "modal", and the child will not be able to receive focus.  If the child form has had its owner set to the parent prior to the parent being shown, an exception gets thrown when attempting to show the parent.  I cannot find decent documentation on all these issues.</p>