title: Cannot access a disposed object C# (showdialog dispose)
body: <p>I am new to c# and kind of winging it. using Microsoft Visual C# 2010</p>
  
  <p>I have checked many similar posts and none of the suggestions seem to help</p>
  
  <p>I am getting the following error: "Cannot access a disposed object"
  which references the main form here</p>
  
  <pre><code>private void btn_RunPkgs_Click(object sender, EventArgs e)
          {
              RunPackages rp = new RunPackages();    
              this.Hide();
              rp.ShowDialog();//The error points to this line
              this.Show();
          }
  </code></pre>
  
  <p>here is the code that blows up when the security check fails.</p>
  
  <pre><code>private void securityCheck()
          {
              if (MyGlobals.FormCheck("RUN_JOBS") == 1)
              {
                  InitializeComponent();
              }
              else
              {
                  //this.BeginInvoke(new MethodInvoker(this.Close));
                  //this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                  MessageBox.Show("You do not have permission to access this form!");
                  //this.Close();
                  this.Dispose();
              }            
          }
  </code></pre>
  
  <p><strong>EDIT</strong>
  It looks like I am going to go with Adriano Repetti's idea of putting the security where I call the page, but I am a little nervous now having any security on the page. </p>
  
  <pre><code>   private void btn_RunPkgs_Click(object sender, EventArgs e)
          {
              if (MyGlobals.FormCheck("RUN_JOBS") == 1)
              {
                  RunPackages rp = new RunPackages();
                  this.Hide();
                  rp.ShowDialog();
                  this.Show();                
              }
              else
              {
                  MessageBox.Show("Not for You!");             
              }
          }
  
          private void btn_ListUpdater_Click(object sender, EventArgs e)
          {
              if (MyGlobals.FormCheck("MDM") == 1)
              {
                  ListUpdater lu = new ListUpdater();
  
                  this.Hide();
                  lu.ShowDialog();
                  this.Show();
              }
              else
              {
                  MessageBox.Show("Private!");
              }            
          }
  </code></pre>
  
  <p><strong>EDIT2</strong>
  Came up with the following possible solution but am nervous to use it because I am new at this and don't know what issues there might be. Any problems with just creating an event handler for form load?</p>
  
  <pre><code>namespace RunPackages
  {
      public partial class ListUpdater : Form
      {
          public ListUpdater()
          {            
              InitializeComponent();
              this.Load += new EventHandler(securityCheck);
          }    
  
          private void securityCheck(object sender, EventArgs e)
          {
              if (MyGlobals.FormCheck("MDM1") == 0)
              {
                  MessageBox.Show("Not Allowed!");
                  this.Close();
              }    
          }
  </code></pre>