|
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:
Each application will run only one copy of itself at a time (it prevents a second copy from starting up). The code the does this is in the top of the Main() method, in the Program.cs file. In my opinion, many applications ought to prevent multiple copies from running, with the possible exception of editors and paint programs.
Each application remembers form position and size between runs. This requires retrieving information in the Form_Load() event handler on program startup, and saving information in the Form_Closing() event handler on the way out.
There are File...Exit menus and Help...About menus, and there is an About window which tells who wrote the program and what it does.
The icon and background is consistent on all forms and on the .exe file.
Persistent settings such as form size or background color are accessible only through "properties" (getters and setters) to preserve encapsulation. These properties are exposed by the Program.cs class. Use of properties makes code more maintainable over time, and it hides the details of where the settings are really stored from the rest of the classes.
COMPILE THE STARTER CODE:
|
![]() Above, I have made "backgroundGradient" the Startup Project. |
CUSTOMIZE THE PROJECT:
|
|
| 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. |
![]() |
|
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 |
| ||
| 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 |
| ||
| 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)
Visitors: