title: How to position UserControl inside of a Panel when it is added at run-time?
body: <p>I have a dialog form with a mixture of design-time and run-time added controls. Each of these controls is hosted in a Panel. Each panel has FillStyle.Top set.</p>
  
  <p>At run time I add a UserControl to a panel:</p>
  
  <pre><code>public NetworkDiscoveryDialog(CSNetworkDiscovery networkDiscovery) : this()
  {
      NetworkDiscovery = networkDiscovery;
      SnmpCommunitiesUserControl = new SnmpCommunitiesUserControl(NetworkDiscovery.SnmpCommunitiesSetting);
      panel2.Controls.Add(SnmpCommunitiesUserControl);
  }
  
  public partial class SnmpCommunitiesUserControl : UserControl
  {
      public SnmpSetting SnmpSetting { get; set; }
  
      public SnmpCommunitiesUserControl()
      {
          InitializeComponent();
          Anchor = (AnchorStyles.Top | AnchorStyles.Left);
          Dock = DockStyle.Fill;
      }
  
      public SnmpCommunitiesUserControl(SnmpSetting snmpSetting)
          : this()
      {
          SnmpSetting = snmpSetting;
      }
  }
  </code></pre>
  
  <p>Unfortunately, SnmpCommunitiesUserControl does not have the same position as the controls which were added at design-time.</p>
  
  <p>I have tried:</p>
  
  <ul>
  <li>Setting the Panel's margin as well as well as padding.</li>
  <li>Setting the UserControl's margin as well as padding.</li>
  <li>Anchor and Dock settings.</li>
  </ul>
  
  <p>None of these seem to have any effect on the run-time added UserControl.</p>
  
  <p>Previously, I was passing a 'Location' parameter to my UserControl's constructor. This allowed me to set the Location of the UserControl, but was not a maintainable solution.</p>
  
  <p>How should I be going about doing this?</p>
  
  <p>EDIT:</p>
  
  <p>My current solution is to nest a a second panel inside of the first panel. I clear all margins and padding for both panels. The parent panel has Dock.Top with its child have Dock.Fill. Then, I set the child's Left/Right padding to 10. This causes the run-time added control to appear in the proper place. I'm not happy with this solution, though.</p>
  
  <p><img src="https://i.stack.imgur.com/FMTPF.png" alt="enter image description here"></p>