Reusable Windows Application Tutorial
 
Specification version 1.0 , last updated 08/07/2006
 

The goal of this tutorial is to provide you with starter code that provides some of the "polish" which Windows applications are typically expected to have.  To use this code as starter code for new projects, you'll need to khow how to change the namespace, executable file name, icons, colors, background image and programmer name (instructions follow). 

Screen shots of three applications, provided to you in one solution folder, are shown below.  They are alike except for the type of background used in forms.  In Windows programs, you should strive for consistent icons, basic menus, an About form, and a reasonable assembly filename that matches the purpose of the program. These applications are examples of my style; you may develop your own.  Background colors or images are fun, but not required.

You may want to compare the above applications with what you get just by running the C# Windows Application wizard for a new project in VS.NET.  Features added by me include:

COMPILE THE STARTER CODE:

  1. Download and unzip standardApplications.zip.   Double click file standardApplications.sln to launch the solution.
  2. In VS.NET's Solution Explorer, expand whichever of the three projects you are going to modify.  Right-click on the project name and select "Set as Startup Project".  This will bold that project name and unbold the other two projects.  The bolded project will be the one that executes in VS.NET when you click Run.
  3. Build and run the project in Debug mode.

Above, I have made "backgroundGradient" the Startup Project.

CUSTOMIZE THE PROJECT:

  1. In the VS.NET Solution Explorer pane, select and then right-click the project name; in the popup menu, select Properties to display the project property pane.  In the Application tab, make these changes:
     
    • Change the Default Namespace here and in all your .cs files.
    • Change the Assembly Name.  Please use one word without spaces or special characters.
    • Rebuild the application.
     

  2. Edit the AssemblyInfo.cs file as follows:
    • Change the AssemblyTitle and AssemblyProduct attributes to reflect the same assembly name you chose in the previous step.
    • Substitute your name for mine in the AssemblyDescription attribute.
    • Change the version of the program to 1.1.0.0.
  3. Build and run the project in Debug mode.  Exit the program.
  4. In Windows Explorer, drill down into the folder where the project file is, then navigate to the \bin\Debug folder.  There you should see the executable file Newname.exe
  5. Persistent user settings are stored on the disk by serializing file Settings.settings (in the Properties folder) to an XML file named user.config.  With patience, you can find the user.config file on the file system.  Using Windows Explorer, browse to the stored settings file in your user file folders and double click on it to examine its contents.  My file path starts with
       C:\Documents and Settings\pgpalmer\Local Settings\Application Data\University_of_Pennsylvania\
    Additional path is created by the system (such as
       Newname.vshost.exe_StrongName_xdf0lknv0ddah4tn0n4kh33eyxd3ije3\1.1.0.0
    Your path will have your login name in place of pgpalmer, and the University_of_Pennsylvania part comes from the AssemblyCompany value in file AssemblyInfo.csRe-examine the user.config file after running your program and resizing the form--you should see changed values stored in it after the application closes.
  6. Change the icon and background color of the application as follows:
     
    If in the application that uses an image background, choose a new image file from background images.  Choose a new icon from icons files (or find your own elsewhere).  Rename the chosen image to background.jpg and the icon to application.ico, and put them in the project folder in place of the original files.  Rebuild the project.  This should change the icon displayed by MainForm.cs and AboutForm.cs, and when using an image as background, each form should use the new image you selected.

  7.  
  8. Open the Project Properties pane (see Step 1 of "Customizing", above) and change the Application Icon by browsing to the new icon (in your application folder).  Rebuild the project.  This should change the icon on the executable file in the \bin\Debug folder.  This file's icon should always be made to match the icon shown on the forms when the program runs.
  9. For application "backgroundColor", change the default solid color which will be used by the application in file Settings.settings (in the Properties folder)
  10. For application "backgroundGradient", change the two colors which will be used by the application to create gradients in file Settings.settings (in the Properties folder)
    You may wish to run the gradient picker to choose two colors which look nice together.
    (You may need to recompile gradient picker on your system, as this version targets the Beta 2 runtime and not the official .NET runtime).

READ THESE CLASSES:
 
AboutForm.cs

and

AboutForm.Designer.cs
(this code was generated by the Windows Designer)

Display this form by selecting About...Help in the main form menu.
AssemblyInfo.cs Metadata about this application and assembly.  This is also a good place to keep a history of version changes in a list of comments.
Form1.cs

and

Form1.Designer.cs
(this code was generated by the Windows Designer)

This form is launched from the static Program.Main() method.
Program.cs Program.cs is a static class that contains the Main() method which is the application entry point.  It contains static properties (getters and setters) for data, and shared methods which may be needed by any forms and other classes.  Some of the properties persist when the program stops and restarts; others are "hardwired" and revert to their default values when the program restarts.  Data is obtained identically from the Program class, whether it is persistent or not; the Program class hides the details of how persistence is accomplished .
Settings.settings The Model class retrieves persistent options from this file, and saves to this file when options change.

SOME QUESTIONS TO ANSWER ABOUT THE CODE:  (these might be on a quiz)

  1. Where does the program start executing?  What objects are created first?  What other startup actions take place?
  2. How does the application prevent multiple copies of itself from running?
  3. What initializations take place during loading of the form?
  4. What configuration options are remembered when you close the program and restart it?  Where (on the disk) is this information being kept when the program is not running?
  5. What actions are taken as the form is about to close?
  6. What does the code in the MainForm.OnPaintBackground() method accomplish?
  7. Why is Program.cs a separate class, and not just part of Form1.cs?
  8. What actions are taken to create a background color or graphic?

Visitors:  Hit Counter