title: How do I dynamically add controls into windows form splitter control while window is minimized?
body: <p>I was tracking down a bug in a small .NET app written here at our studio.  The dialog is basically a status reporting dialog that tries to dynamically add 1 (or more) progress bars to an existing dialog depending on the number of operations that are currently being logged.</p>
  
  <p>We have a dialog that has a main dialog that contains a splitter container.  The lower part of the splitter is tied to a rich edito control that shows status spew.  The top portion of the splitter area is where we want to dynamically add 1 (or more) progress bars.  </p>
  
  <p>During operation, new progress bars are created and then attached to the split container by adjusting the split containers panel 1 size to account for the new control, and then the new control is then added to the split container (code is shown below).  This code works great is the window is not minimized.  When the window is minimized, an exception is thrown at runtime which indicates that the split container size cannot be adjusted.  Specifically, here is the error message:</p>
  
  <pre><code>An exception of type 'System.InvalidOperationException' has occurred.
  Message: SplitterDistance must be between Panel1MinSize and Width - Panel2MinSize.
  </code></pre>
  
  <p>The problem (I think) is that because the window is minimized, the height of the window is too small to allow the split container's size to be adjusted.  I suspect that .NET is looking at the windows minimized size instead of it's normal state size.</p>
  
  <p>Here is the code (note the line that throws the exception):</p>
  
  <pre><code>// if we don't have any operations yet, add a small top margin
  if (splMain.SplitterDistance == 0) {
      splMain.SplitterDistance = 3;      // THE EXCEPTION IS THROWN HERE!!!!
  }
  
  ProgressDisplay display = new ProgressDisplay();
  display.OperationFinished += OperationFinished;
  display.Anchor    = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
  display.LabelText = label;
  display.Location  = new Point(6, splMain.SplitterDistance);
  display.Size      = new Size(splMain.Panel1.Width-12, display.Height);
  display.Start(steps);
  m_Operations.Push(display);
  
  // add the display to the top panel
  splMain.IsSplitterFixed = false;
  splMain.SplitterDistance += display.Height + 3;
  splMain.IsSplitterFixed = true;
  splMain.Panel1.Controls.Add(display);
  m_splitterAdjusted = true;
  </code></pre>
  
  <p>According to the debugger, panel1MinSize is 0, panel2MinSize is 2, and Width, and the size of the parent window is (160,25).  Because the splitter is a horizontal splitter, I suspect that "Width" in the exception is really height, which is 25, making the exception be SplitterDistance (3), must be beteween 0 and 0 - hence the exception.</p>
  
  <p>For now, I just don't add the progress bars if the window is minimized, but this of course has the downside of not showing the progress bars when the window is restored.  I guess another solution is to add the progress bars on a restore event (if they haven't been added yet).</p>
  
  <p>I'm curious if there is a better (or different) solution that might be available.  Thanks!</p>