Programming
Assignment Animals
Purpose
Practice
using the most basic Windows controls and practice programming a simple modal
dialog box. In addition you will
practice dynamically adding an item to a menu.
The program stores data on
different animals. It can display this
data to the user and allow the user to add a new animal or edit an existing
animal. Screen shots of a working
program are provided below.
Specifications
1. The main window always displays a picture
of, and some data about, an animal. The
data to be displayed are the common name, the scientific name, and the diet of
the animal.
2. There is a Display menu; this always lists the common names of the animals
whose data are available. If you select
an animal from this menu,
it becomes the currently selected animal (whose data is displayed
in the main window).
3. There is an Animals menu; this has two menu items, Edit and Add New Animal. The Edit
option brings up a modal dialog with title Edit Animal with four text boxes that contain the data for the
currently selected animal: the common
name, the diet, the scientific name, and the filename of the image of this
animal. These text boxes are preceded by
labels identifying the data to be entered. The AddNewAnimal option brings up the
same dialog, but with blank edit boxes and the title New Animal. When you press
OK on the Edit dialog, the changed
data replaces the data for the currently selected animal. When you press OK on the AddNewAnimal dialog, a new entry is created for the new
animal, and its common name is added to
the Animals menu, at the end, so that the Animals menu always appears in the order that animals were added. Also, the data for the new animal, including
the picture, is displayed. When you press
Cancel on either dialog, no change
results—the situation is exactly as it was before the dialog came up.
4. One “default” animal is
pre-loaded, so that a picture of an animal comes up when the program
starts. In order to keep down the size
of the executable file, please do not pre-load more than one animal. You will submit, as always, only your .exe
file; the grader will add new animals using image files on his computer. Note,
even though this animal’s picture is built in, the user can still edit
this entry and change the image file just like any other entry.
5. Error checking (data validation) is not
required on this assignment. If someone enters blank or nonsensical data, or a non-existent
filename, too bad. This is an
exercise in the basic method of bringing up a modal dialog. While data validation is also important, it
is a more advanced issue not covered in this assignment.
6. When images of various sizes are loaded, the
display is still acceptable:
(a) we never
have a large image obscuring the common name, scientific name, or diet;
(b) all the data is always
visible (It is OK if large images are clipped to a reasonable size);
(c) and the
screen layout should be acceptable.
7. For an A+:
also implement a Delete menu
item under the Animals menu. The effect of Delete is to remove the currently selected animal. Its name should also disappear from the Display menu. The currently selected animal should change
to the default animal (see specification 4).
The default animal itself cannot be deleted; if you try, either nothing
happens or you get an error message.
Grading Criteria
1. When the program opens, the initial display
is correct: photo, common name,
scientific name, and diet are displayed as illustrated. (10 points)
2. The menus appear correctly. (10
points)
3. The Edit
and Add New Animal items bring up a
modal dialog with the specified text boxes and labels. (15 points)
4. The modal dialog box has the correct title, no
maximize or minimize box or control menu, no border,
and is not resizable. (5 points)
5. The Edit
dialog is initialized with the data of the currently selected animal; the Add New Animal dialog is initialized
with blank text boxes. (10 points)
6. When OK is pressed on either dialog, using
valid data: the new data describes the currently selected animal and is
displayed. (10 points). In the case of the Add New Animal dialog,
that means that the new animal is displayed. In particular the placement of the text information
is below the picture as shown in the screen shots, so its exact location
depends on the size of the picture—a big picture should not overwrite the
data and a little picture should not result in a huge blank space, and the form
itself should be large enough to accommodate both.
7. In addition, when OK is pressed after
editing the common name of the animal, its name changes on the Display menu. (10 points)
8. When OK
is pressed in the Add New Animal
dialog, a corresponding item correctly appears on the Display menu
(10 points)
9. The menu functions correctly, to display the
data of the selected animal. (10
points)
10. Nothing happens if Cancel is pressed in the Edit
and Add New Animal dialog. (5 points, only if OK works as in numbers 5 and
7)
11. When images of various sizes are loaded, the
display is still acceptable. (5 points).
12. If you got all the items above (and only then)
you can get an A+ by implementing Delete
as described in the specifications. (As
always, the A+ level feature cannot be used to make up for defects in the A
level work. It won’t even be tested unless you’ve earned all the
other points.)
Screen shots. In order, the opening screen; adding a new animal; the result of adding a
new animal.



Programming Hints
1. First make your new project. Call it Animals. The submission system will ask for Animals.exe. Make sure your title bar, however, mentions
your 4-digit student ID as shown in the screen shots.
Then start by collecting a few images of animals
from the Web. Go to www.google.com and select Images.
You’ll find pictures of lots of animals by searching for the
animal by name. Then save the images
you like to your hard disk, in the subfolder bin\Debug of your project folder
(where you also will see the .exe file once it exists. You may have to build the program once before
this folder exists.) Warning:
In 2006 I tested this with Mozilla
Firefox and it worked fine with the current version, but in 2005 Firefox the then-current version did not
work correctly and I had to use IE to save a .jpg file. Anyway, after downloading a few images, check
that they are good images by double-clicking them in Windows Explorer to see if
they display correctly. That way,
you’ll know that any problems are your own bugs, and not due to bad image
files.
You
might as well google up the scientific name of your default animal too. Note: although
I’ve suggested collecting several images for your own testing, please
build only one not-too-huge image into your program, to keep down the size of
the executable file. Also, don’t use the same image as your
friends, as two programs with an
identical default image will be examined more closely for evidence of
copying, though I realize it could
easily happen innocently through the use of Google.
2. Define a class Animal. Use the Visual
Studio menu item Project | Add Class. You’ll need using System.Drawing so it will recognize
Image.
public String commonName;
public String diet;
public String scientificName;
public String photofile;
public Image picture;
Write
a constructor that constructs an animal.
Mine takes the first four fields as input and uses the Image.FromFile
method to get picture, like this:
this.picture = Image.FromFile(photofile);
3. In your Form1 class, define
private Animal[] m_Animals;
private int m_DisplayedAnimal;
// which entry of m_Animals
to display in the main window
private int m_nAnimals; // number of animals for which we have stored
data
The job of the program is to display the data of the
animal described by m_Animals[m_DisplayedAnimal].
4. Add a PictureBox to your form, to display the photo of the animal. Browse for the image you want for your default animal and specify it as the image for this picture box.
5. To get started, you’ll need to initialize m_Animals[0] to valid data. You don’t want to use the constructor of Animal that takes a filename, because this entry is special: its Image is the one you specified for your picturebox:
m_Animals[0].picture =
pictureBox1.Image;
m_Animals[0].photofile = ”built in”;
At this point you should be able to run your program and see your default animal’s picture.
6. Now add code to display the common name, scientific name, and diet of the animal in question. You can design this display any way you please. But watch out: the images of the animals are of unknown size, so eventually you have to pay attention to specification 6. You may find the following lines of code helpful:
Font
roman = new Font("Times
New Roman",12);
Font italic = new Font(roman,FontStyle.Italic); // create an italic version of the first font
7. Now, following the lecture notes, create your modal dialog. The same dialog can serve for Add New Animal and Edit.
8. Add your menu. To start with there is only one animal listed, your default animal. Add handlers for the menu items. Following the lecture notes, add code for the Edit and the Add New Animal items, to properly initialize and bring up your modal dialog. Add code in your menu handler to properly change the Form1 data after OK is pressed.
9. When a new animal is added, you will need to add a new item to the menu. This new item will need a handler. If you write the handler correctly, all the items on the Display menu can use the same handler. Here is an example handler:
private void
LionItem_Click(object
sender, System.EventArgs e)
{ ToolStripMenuItem mi = (ToolStripMenuItem)
sender;
m_DisplayedAnimal =
mi.MergeIndex;
Invalidate();
}
Note that this handler depends on keeping the animals in the same order in the menu as in the m_Animals array. If you try for the A+ this will get more complicated.
Here are three lines of code from the handler for the new animal menu item that you may find helpful. In this code, a is a newly created Animal object.
ToolStripMenuItem mi = new ToolStripMenuItem(a.commonName, null,
new System.EventHandler(LionItem_Click));
mi.MergeIndex = m_nAnimals;
menuItem1.DropDownMenuItems.Add(mi);
10. When an old animal’s common name is changed, you will need to change the corresponding item on the menu.