ARTICLE

Filling out 1040EZ Tax Form in .NET

Posted by Mike Gold Articles | Visual Basic 2010 June 25, 2003
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 article shows you how to print out your own 1040EZ form.
 
Reader Level:

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, simply simply cycling through the controls of the form does all the drawing, printing and saving of information.  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:

Listing 1 - Saving the 1040EZ form contents to an ASCII file

Private Sub SaveMenu_Click(sender As Object, e As System.EventArgs)
' 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 Then
' Create a file stream with the name of the selected filename
Dim fs As New FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write)
' Use the Stream Writer to write out the information of the 1040EZ form
Dim sw As New StreamWriter(fs) ' Cycle through each control in the form.
Dim c As Control
For Each c In Controls
' Determine the type of control. For this form we only read TextBoxes and CheckBoxes
Dim strType As String = 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" Then
sw.WriteLine(CType(c, TextBox).Text.ToString())
' If the control is a CheckBox, write out a 1 if its checked, 0 if its not
Else
If
strType = "CheckBox" Then
If
CType(c, CheckBox).Checked Then
sw.WriteLine("1")
Else
sw.WriteLine("0")
End If
End
If
End If ' Close the stream
Next c
sw.Close()
fs.Close()
End If
End
Sub 'SaveMenu_Click

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 it's 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 Sub DrawAll(g As Graphics) '
' 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
Me.AutoScrollPosition = New Point(0, 0) ' Set up a source rectangle based on the image size
Dim srcRect = New Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height) ' Set up a destination rectangle based on the paper size
Dim nWidth As Integer = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Width
Dim nHeight As Integer = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Height
Dim 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.
Dim scalex As Single = destRect.Width / srcRect.Width
Dim scaley As Single = destRect.Height / srcRect.Height ' Cycle through each control and draw its contents
Dim c As Control
For Each c In Controls
' Determine the control type
Dim strType As String = c.GetType().ToString().Substring((c.GetType().ToString().LastIndexOf(".") + 1))
If strType = "TextBox" Then
Dim
theText As TextBox = CType(c, TextBox)
' 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" Then
g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top * scaley, New StringFormat())
Else
Dim
LayoutRect As 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)
End If ' if the control is a checkbox and checked, draw an x inside
End If
If
strType = "CheckBox" Then
Dim
theCheck As CheckBox = CType(c, CheckBox)
Dim aRect As Rectangle = theCheck.Bounds
If theCheck.Checked Then
g.DrawString("x", theCheck.Font, Brushes.Black, theCheck.Left * scalex + 1, theCheck.Top * scaley + 1, New StringFormat())
End If
End
If
Next
c ' end foreach control}
End Sub 'DrawAll

Improvements

One thing I noticed is that would be nice to have is 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.

share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor