title: Lazy load forms
body: <p>I have a .NET CF program (running on a Smartphone) and I have written an options page for it.</p>
  
  <p>On the options page I have links to 6 other forms which contain the actual options.
  When the main form loads it creates the 6 subforms and stores them in a list ready to be launched.  The issue with this is the initial creation time required to make these forms, which makes the main settings form slow.</p>
  
  <p>What I would ideally like, is a way to just specify the type and parameters required to create the subform on load, then actually create the form when needed.</p>
  
  <p>Here is code snippets of how it currently works:</p>
  
  <p>Form_OnLoad():</p>
  
  <pre><code>AddSettingsOptions("General", new General());
  AddSettingsOptions("Action Alert", new BaseAlertForm("Action Alert Settings", SystemManager.ActionAlert));
  AddSettingsOptions("Comms Alert", new BaseAlertForm("Comms Alert Settings", SystemManager.CommsErrorAlert));
  AddSettingsOptions("Advanced", new Advanced());
  AddSettingsOptions("Diagnostics", new Diagnostics(_unitWatcher));
  AddSettingsOptions("About", new About());
  </code></pre>
  
  <p>The form is stored in the Tag section of a list (which displays the sub options)</p>
  
  <pre><code>private void AddSettingsOptions(String name, Form form)
  {
      listViewSettings.Items.Add(
          new ListViewItem(
              String.Format(" {0}   {1}", listViewSettings.Items.Count + 1, name))
              {
                  Tag = form
              }
          );
  }
  </code></pre>
  
  <p>If an option is pressed the following function is called to launch the form</p>
  
  <pre><code>private void ShowSubSetting(ListViewItem item)
  {
      if (item == null)
          return;
  
      object tag = item.Tag;
      Form form = tag as Form;
      if (form != null)
      {
          form.ShowDialog();
          //form.Dispose();
      }
  }
  </code></pre>
  
  <p>I'm sure there is a pretty easy way to do this, I'm just struggling with the correct way.
  Thanks</p>