|
Reusable Windows Application
version 2.2
, last updated
07/15/2006 - fixed
resizing after maximize
|
This application doesn't do much of anything except display the time and
date, but its code gives you a way to keep persistent settings, change the
background style, and keep an application log file:

The goal of this project is to provide you with code samples that you can use when writing Windows
applications in the future, and allow you to practice customizing an application.
It also illustrates use of a timer (for the clock). You must turn in a slightly modified version of this program in which you have
changed the namespace, executable file name, icon, colors, background
image and programmer name (instructions follow). Please read and study
this code thoroughly; it is the foundation for future projects.
Please compare this program with
what you get just by running the C#
Windows Application wizard for a new project in VS.NET. Here are some
of the features which have been added:
-
A log file for capturing information about usage of the
application
-
An XML settings file which can store user options required
to persist between runs of the application
-
Optional gradient backgrounds in forms
-
An About form
-
A "model" object which stores data that is persistent or is
needed by multiple objects--all data in the model is static and can be
accessed only through "properties" (getters and setters)
COMPILE THE STARTER CODE:
- Create a folder with your email ID as its name, and unzip
StdWinApp_v22.zip into
that folder. Double click file StdWinApp.sln to launch the solution.
- In VS.NET, select View Solution Explorer; you should see the following
files in the project:
-
In VS.NET, right click over References. If they are not already
present, you may need to add references to the System.Runtime.Serialization.Formatters.Soap.
-
Build and run the project in Debug mode.
|
 |
CUSTOMIZE THE PROJECT:
-
Select and then right-click the project name; in the popup menu, select
Properties
to get the project
Property Pages
popup window. In the
Common Properties
folder and the
General
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.
-
Save any open files.
-
Change the Startup Object to reflect the new namespace.
- Rebuild the application.
|
|
|
- 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 2.3.0.0.
-
Build and run the project in Debug mode. Exit the program.
-
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
-
The log file Newname_log.txt and the settings file Newname_settings.xml. should
be located in a special folder (accessed in code by the object "Application.Local");
the path for me is C:\Documents and Settings\pgpalmer\Local
Settings\Application Data\Upenn\Standard\1.6.0.0; yours will be similar, but
pgpalmer will have your login name instead. In that folder, double click on the log and settings file to
examine their contents. The log and settings files should reflect the
actions you took when you ran the program. Please now delete the old files (those which begin with
standard) from the
folder. The new ones created when you run the application should have a
different name if you have successfully renamed the program.
- Change the icon and background color of the application as follows:
|
Choose a new image file from
background images and 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, it should use the new image you
selected. |
 |
- Open the Project Properties pane (see Step 1
of "Customizing", above) and change the
Application Icon by browsing to
the new icon (wherever it currently is on your file system). 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.
- Change the default solid color which will be used by the application
(HINT: The change must take place in Model.cs).
- Change the default gradient colors which will be used by the application
(HINT: The change must take place in Model.cs).
|
You may wish to run the gradient picker to choose two
colors which look nice together. |
 |
- Compile and test thoroughly before zipping up the folder and turning it
in.
READ THESE CLASSES:
|
AboutForm.cs
and
AboutForm.Designer.cs |
 |
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. |
|
LogFile.cs |
This class manages the log file. It is largely
static; it must be initialized once
by calling LogFile.InitLogFile(),
and thereafter, one can call LogFile.WriteLine("string") from anywhere within the project. When the
program closes, it is important to call LogFile.Close() to make sure the
file closes. |
|
MainForm.cs
and
MainForm.Designer.cs |
 |
This form is launched by the static Model.Main() method after it has
initialized the data for the
application. |
|
|
Model.cs |
Model
is a Singleton
object. Model.cs also contains the Main() method which is the
application entry point. Model.cs contains (mostly) 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 revert to their default values.
Data is obtained identically from the Model, whether it is persistent or
not; the Model hides the details of how persistence is accomplished . |
|
Persistent.cs
|
Persistent is a
Singleton object,
and it should be accessed only from within the Model class. The Model stores all
remembered settings in the Persistent object and serializes or deserializes the
Persistent object to/from an XML settings file on disk as needed.
|
SOME QUESTIONS TO ANSWER ABOUT THE CODE: (these might be on a
quiz)
- Where does the program start executing? What objects are created
first? Are they Singletons? What other startup actions take
place?
- What initializations take place during loading of the form?
- What is the timer on the form doing? What is its firing interval,
and why?
- If I run this application for more than 24 hours, will the date in the
lower right of the status bar change on the following day? Why or why
not?
- What things about the program are remembered when you close the program
and restart it? Is form location and size remembered? Is the
background type retained? Where is this information being kept on the
disk when the program is not running? How is this information being
saved? What is serialization?
- Where is the log file (written by
LogFile.cs)? What is in it? Why might you want to
have a file like this for your application?
- Will the log file grow to be really large someday and fill up the entire
disk?
- What actions are taken as the form is about to close?
- What does the code in the Form1.OnPaintBackground() method accomplish?
- Why is Model.cs a separate class, and not just part of Form1.cs?
- In what folder are the log file and XML
settings file (serialized from Persistent.cs) stored?
- What actions are taken when the user changes the background color or
graphic?
Visitors:
