title: Form.ActiveForm occasionally works
body: <p>I know it's poor design, but I've been transferring control of my GUI Form Class to other classes in my C# program.</p>
  
  <p>This has been working fine for a long time now, using:</p>
  
  <pre><code>var form = Form.ActiveForm as Form1;
  </code></pre>
  
  <p>As my program is growing bigger, and the form is opened by more classes, it's behavior becomes random. I can sometimes run my program through to fruition with no errors, but other times it will pick a random spot and throw a NullReferenceException.</p>
  
  <p>For instance, the following two lines of code are from the eight function that is called. </p>
  
  <pre><code>var form = Form.ActiveForm as Form1;
  form.richTextBox1.AppendText("Section ID: ");
  </code></pre>
  
  <p>Since the program got to this point, I know that it was able to pass the form and manipulate the same field previously. </p>
  
  <p>Any ideas as to why it choses to throw it, when the exact same thing worked previously? </p>
  
  <p>As far as I know Form1 is the only form. I am 100% green to GUI development, and C#/.NET, so I am likely wrong here.</p>
  
  <p>One theory I have is as follows: There are two classes, Reader, and Analyze, that inherit the Form object. Reader inherits it first with no problem. The problem is immediately thrown in Analyze. Am I not relinquishing the form in the Reader class? </p>
  
  <p>Edit: My first hunch was that having multiple classes using the ActiveForm property was causing issues. I put all of my functions into the Reader class. This didn't solve the issue. </p>
  
  <p>Below are two methods in Reader. The first, read() is called before SectionHeaderMatch(). Read uses the ActiveForm property and never has a problem, as does another method called before it. SectionHeaderMatch is the third method to use the ActiveForm property. In this method, the form is set to null, instead of Form1. I'm assuming something in read() is screwing things up.</p>
  
  <pre><code>public static void read()
      {
          var form = Form.ActiveForm as Form1;
  
          StringBuilder outPut = new StringBuilder();
          outPut.Append(form.textBox2.Text);
          outPut.Append("\\out.txt");
  
          string cardDrive = String.Format("\\\\.\\{0}", form.textBox1.Text);
          cardDrive = cardDrive.Remove(6);
          SafeFileHandle fileHandle = CreateFile(cardDrive, 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
          FileStream readStream = new FileStream(fileHandle, FileAccess.Read);
          BinaryReader reader = new BinaryReader(readStream);
          FileStream writeStream = File.OpenWrite(outPut.ToString()); //Writing stream opened at the specified directory of output.
  
          long gigsRead; //Loop counter that specifies the number of gigabytes read thus far.
          long megsRead; //Loop counter that specifies the number of megabytes read thus far within the current gigabyte.
  
          for (gigsRead = 0; gigsRead &lt; 0; gigsRead++)
          {
              for (megsRead = 0; megsRead &lt; 1024; megsRead++)
              {
                  byte[] buffer = new byte[1048576];
                  long position = gigsRead * 1073741824 + megsRead * 1048576;
                  reader.BaseStream.Position = position;
                  reader.Read(buffer, 0, 1048576); //Read from SD card to buffer
                  writeStream.Write(buffer, 0, 1048576); //Write from buffer to output text file.
                  writeStream.Flush();
              }
          }
  
          for (megsRead = 0; megsRead &lt; 432; megsRead++)
          {
              byte[] buffer = new byte[1048576];
              long gigSize = 1073741824;
              long position = 7 * gigSize + megsRead * 1048576;
              reader.BaseStream.Position = position;
              reader.Read(buffer, 0, 1048576); //Read from SD card to buffer
              writeStream.Write(buffer, 0, 1048576); //Write from buffer to output text file.
              writeStream.Flush();
          }
          writeStream.Close();
          readStream.Close();
          reader.Close();
          fileHandle.Close();
          outPut.Clear();
      }
  
      public static void SectionHeaderMatch()
      {
          var form = Form.ActiveForm as Form1;
          if (form == null)
          {
  
          }
  
          else
          {
              StringBuilder outPut = new StringBuilder();
              outPut.Append(form.textBox2.Text);
              outPut.Append("\\out.txt");
  
              FileStream readFile = new FileStream(outPut.ToString(), FileMode.Open, FileAccess.Read);
              BinaryReader readerFile = new BinaryReader(readFile);
  
              //Sector 1
              readerFile.BaseStream.Position = 1073741824;
              byte[] sectorOne = new byte[Form1.blockSize];
              readerFile.Read(sectorOne, 0, Form1.blockSize);
  
              //Sector 2
              readerFile.BaseStream.Position = 1073741824 + Form1.blockSize;
              byte[] sectorTwo = new byte[Form1.blockSize];
              readerFile.Read(sectorTwo, 0, Form1.blockSize);
  
              readerFile.Close();
              readFile.Close();
  
              string sector1 = UtilityClass.ByteArrayToString(sectorOne);
              string sector2 = UtilityClass.ByteArrayToString(sectorTwo);
  
              if (String.Compare(sector1, sector2) == 0) //0 if they match
              {
                  if (headerMatchRunCount == 0) //If this is section 1
                  {
                      form.richTextBox3.AppendText("Section 1 headers match.");
                  }
  
                  else //Section 2
                  {
                      form.richTextBox4.AppendText("Section 2 headers match.");
                  }
              }
  
              else //Headers did not match
              {
                  if (headerMatchRunCount == 0) //Section 1
                  {
                      form.richTextBox3.AppendText("Section 1 headers match.");
                  }
  
                  else //Section 2
                  {
                      form.richTextBox4.AppendText("Section 2 headers match.");
                  }
              }
          }
      }
  </code></pre>