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

private void SaveMenu_Click(object sender, System.EventArgs e)
{
// Set the initial directory of the save file dialog to the same path as the application
saveFileDialog1.InitialDirectory = Application.ExecutablePath.ToString();
// Check to see if the user picked a file name to save the form information
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// Create a file stream with the name of the selected filename
FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);
// Use the Stream Writer to write out the information of the 1040EZ form
StreamWriter sw = new StreamWriter(fs);
// Cycle through each control in the form.
foreach (Control c in Controls)
{
// Determine the type of control. For this form we only read TextBoxes and CheckBoxes
string strType = c.GetType().ToString().Substring(c.GetType().ToString().LastIndexOf(".") + 1);
// If the control is a TextBox, write out the text contained inside to a line in the file
if (strType == "TextBox")
{
sw.WriteLine( ((TextBox)c).Text.ToString());
}
// If the control is a CheckBox, write out a 1 if its checked, 0 if its not
else if (strType == "CheckBox")
{
if (((CheckBox)c).Checked)
sw.WriteLine("1");
else
sw.WriteLine("0");
}
}
// Close the stream
sw.Close();
fs.Close();
}
}

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

private void DrawAll(Graphics g)
{
// Create an alignment object that aligns text to the right side
// for calculations
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
// Force autoscroll to start printing from the origin
this.AutoScrollPosition = new Point(0,0);
// Set up a source rectangle based on the image size
RectangleF srcRect = new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height);
// Set up a destination rectangle based on the paper size
int nWidth = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Width;
int nHeight = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Height;
RectangleF destRect = new Rectangle(0, 0, nWidth, nHeight);
// Draw the bitmap of the blank 1040EZ form
g.DrawImage(HiResImage, destRect, srcRect, GraphicsUnit.Pixel);
// Calculate the scale of the image against the paper so we
// can correctly place the text on top of the image from the
// controls.
float scalex = destRect.Width/srcRect.Width;
float scaley = destRect.Height/srcRect.Height;
// Cycle through each control and draw its contents
foreach (Control c in Controls)
{
// Determine the control type
string strType = c.GetType().ToString().Substring(c.GetType().ToString().LastIndexOf(".") + 1);
if (strType == "TextBox")
{
TextBox theText = (TextBox)c;
// check the alignment of the TextBox. If the Alignment is
// Right Aligned, calculated the Layout Rectangle and draw
// the text right aligned inside the Rectangle
if (theText.TextAlign.ToString() == "Left")
g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left*scalex, theText.Bounds.Top *
scaley,
new StringFormat());
else
{
RectangleF LayoutRect =
new RectangleF(theText.Bounds.Left*scalex, theText.Bounds.Top*scaley,
theText.Bounds.Width*scalex, theText.Bounds.Height*scaley);
g.DrawString(theText.Text, theText.Font, Brushes.Black, LayoutRect, sf);
}
}
// if the control is a checkbox and checked, draw an x inside
if (strType == "CheckBox")
{
CheckBox theCheck = (CheckBox)c;
Rectangle aRect = theCheck.Bounds;
if (theCheck.Checked)
{
g.DrawString("x", theCheck.Font, Brushes.Black,
theCheck.Left*scalex + 1, theCheck.Top*scaley + 1,
new StringFormat());
}
}
}
// end foreach control
}

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.

Up Next
    Ebook Download
    View all
    Learn
    View all