Filling out your 1040EZ Tax Form in .NET

Another year has gone by and some things don't change.  That's right,  paying taxes.  What has changed over the years is that taxes have become more and more complicated.  If you've ever looked at the 1040 Form, the 1099, the W-2, the Schedule A, B, C and the rest of the alphabet, you probably found yourself wondering which is worse:  paying the money to the government or filling out the paperwork.  A few years ago, the government heard the cry of the citizen who was up to his neck in forms and tried to ease the process of filing the 1040  by creating the 1040EZ form.  This form, which is miraculously only a single page,  is a lot less painful than its descendant the 1040.  Of course if you are able to take advantage of any of the tax benefits the government provides (such as deductions), you'll need to file the old dependable, but tedious 1040.
 
 
Figure 1 - 1040EZ Form Application
 
Since the 1040EZ form is only a single page,  I thought it would be fun to create a Window Form application that helped you to type into the form.  This program will allow you to fill out most of the fields and perform the calculations on certain fields as well.  The application also allows you to print the form, print preview the form, open an existing form, and save your current 1040EZ Form.  This article is similar to a previous article I wrote on Printing out a W-2 Form.  The program in this current article is slightly improved  in that it allows you to scroll through the form by setting the AutoScroll property of the form to true.  This program is also more generic.  Except for the specific calculations for the 1040EZ form, all the drawing, printing and saving of information is done by simply cycling through the controls of the form.  The same code could be used for any form (e.g. a driver's license application).  Below is the code for saving the information in the controls of the form. Note how we cycle through each control in the form.  Then, using the StreamWriter, we write out the content of each control.
 
Another year has gone by and some things don't change. That's right, paying taxes. What has changed over the years is that taxes have become more and more complicated. If you've ever looked at the 1040 Form, the 1099, the W-2, the Schedule A, B, C and the rest of the alphabet, you probably found yourself wondering which is worse: paying the money to the government or filling out the paperwork. A few years ago, the government heard the cry of the citizen who was up to his neck in forms and tried to ease the process of filing the 1040 by creating the 1040EZ form. This form, which is miraculously only a single page, is a lot less painful than its descendant the 1040. Of course if you are able to take advantage of any of the tax benefits the government provides (such as deductions), you'll need to file the old dependable, but tedious 1040.
 
Listing 1 - Saving the 1040EZ form contents to an ASCII file
  1. private void SaveMenu_Click(object sender, System.EventArgs e) {  
  2.  // Set the initial directory of the save file dialog to the same path as the application  
  3.  saveFileDialog1.InitialDirectory = Application.ExecutablePath.ToString();  
  4.  // Check to see if the user picked a file name to save the form information  
  5.  if (saveFileDialog1.ShowDialog() == DialogResult.OK) {  
  6.   // Create a file stream with the name of the selected filename  
  7.   FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);  
  8.   // Use the Stream Writer to write out the information of the 1040EZ form  
  9.   StreamWriter sw = new StreamWriter(fs);  
  10.   // Cycle through each control in the form.  
  11.   foreach(Control c in Controls) {  
  12.    // Determine the type of control. For this form we only read TextBoxes and CheckBoxes  
  13.    string strType = c.GetType().ToString().Substring(c.GetType().ToString().LastIndexOf(".") + 1);  
  14.    // If the control is a TextBox, write out the text contained inside to a line in the file  
  15.    if (strType == "TextBox") {  
  16.     sw.WriteLine(((TextBox) c).Text.ToString());  
  17.    }  
  18.    // If the control is a CheckBox, write out a 1 if its checked, 0 if its not  
  19.    else if (strType == "CheckBox") {  
  20.     if (((CheckBox) c).Checked)  
  21.      sw.WriteLine("1");  
  22.     else  
  23.      sw.WriteLine("0");  
  24.    }  
  25.   }  
  26.   // Close the stream  
  27.   sw.Close();  
  28.   fs.Close();  
  29.  }  
  30. }  
The routine for printing the control is just as generic. In the printing routine we cycle through each control and draw the control contents if its either a TextBox or a CheckBox. The drawing of the form for printing is called from the PrintPage event (More on how to add printing to your applications can be found in the book The Complete Visual C# Programmer's Guide.)
 
Listing 2 - Routine called for printing out the 1040EZ form
  1. private void DrawAll(Graphics g) {  
  2.  // Create an alignment object that aligns text to the right side  
  3.  // for calculations  
  4.  StringFormat sf = new StringFormat();  
  5.  sf.Alignment = StringAlignment.Far;  
  6.  // Force autoscroll to start printing from the origin  
  7.  this.AutoScrollPosition = new Point(0, 0);  
  8.  // Set up a source rectangle based on the image size  
  9.  RectangleF srcRect = new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height);  
  10.  // Set up a destination rectangle based on the paper size  
  11.  int nWidth = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Width;  
  12.  int nHeight = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Height;  
  13.  RectangleF destRect = new Rectangle(0, 0, nWidth, nHeight);  
  14.  // Draw the bitmap of the blank 1040EZ form  
  15.  g.DrawImage(HiResImage, destRect, srcRect, GraphicsUnit.Pixel);  
  16.  // Calculate the scale of the image against the paper so we  
  17.  // can correctly place the text on top of the image from the  
  18.  // controls.  
  19.  float scalex = destRect.Width / srcRect.Width;  
  20.  float scaley = destRect.Height / srcRect.Height;  
  21.  // Cycle through each control and draw its contents  
  22.  foreach(Control c in Controls) {  
  23.   // Determine the control type  
  24.   string strType = c.GetType().ToString().Substring(c.GetType().ToString().LastIndexOf(".") + 1);  
  25.   if (strType == "TextBox") {  
  26.    TextBox theText = (TextBox) c;  
  27.    // check the alignment of the TextBox. If the Alignment is  
  28.    // Right Aligned, calculated the Layout Rectangle and draw  
  29.    // the text right aligned inside the Rectangle  
  30.    if (theText.TextAlign.ToString() == "Left")  
  31.     g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top *  
  32.      scaley,  
  33.      new StringFormat());  
  34.    else {  
  35.     RectangleF LayoutRect = new RectangleF(theText.Bounds.Left * scalex, theText.Bounds.Top * scaley,  
  36.      theText.Bounds.Width * scalex, theText.Bounds.Height * scaley);  
  37.     g.DrawString(theText.Text, theText.Font, Brushes.Black, LayoutRect, sf);  
  38.    }  
  39.   }  
  40.   // if the control is a checkbox and checked, draw an x inside  
  41.   if (strType == "CheckBox") {  
  42.    CheckBox theCheck = (CheckBox) c;  
  43.    Rectangle aRect = theCheck.Bounds;  
  44.    if (theCheck.Checked) {  
  45.     g.DrawString("x", theCheck.Font, Brushes.Black,  
  46.      theCheck.Left * scalex + 1, theCheck.Top * scaley + 1, new StringFormat());  
  47.    }  
  48.   }  
  49.  } // end foreach control  
  50. }
Improvements
 
One thing I noticed is that would be nice to have are some TextBox controls that have masks for phone numbers and social security numbers so dashes and parentheses are automatically placed in the right places. In this application, you need to add spaces to your Social Security Number to have the Numbers fall in the correct spots on the line. Also, the program should also validate eligibility for using the 1040EZ form. (e.g. if you over the age of 65, you can't use the form). What might be cool, if the W-2 Application and the 1040EZ form application could be linked together to extract the form information from the W-2 Application. I think the future of all these government forms is to have these applications running on the web, all linked together similar to the way they are in applications such as Turbo Tax so anyone can access them and file there taxes over the internet. Maybe the .NET initiative will help pave the way for a less complicated and less taxing situation.


Similar Articles