CS
205 - Programming for the Sciences
Spring 2008 - In-class
Exercise for 04/01/08
Today's exercise is to add a menu to the Game of Life program with actions to open (load) a file with a grid configuration, save a configuration to the current file, or save a configuration to another file. For this exercise, you are given a C# project with a complete Game of Life program. You are to add a menu and the handlers implementing the menu actions.
Use a Web browser to go to the course webpage http://csserver.evansville.edu/~hwang/s08-courses/cs205.html. Under today's date, save the file GameOfLifeFilesInClass.zip. Extract the solution folder. Double-click into the folder, then double-click on GameOfLife.sln (the Visual Studio solution file). This will launch Visual Studio with the solution loaded. Save the sample Game of Life configuration file GliderGun.gol from the webpage to the solution folder (the file could go anywhere, but this will make it easier to find).
Changes from the previous Game of Life program:
Textboxes for the number of row and number of columns of the current grid (rowSize and colSize, respectively) have been added along with a Resize button that will read these textboxes and create a new grid of that size.
In order to get all GUI controls laid out where we want them, a panel that covers all but the top edge of the form was added and all of the GUI controls except the menu strip are in the panel. As with the outer form, the panel's AutoSize property is set to True and the AutoSizeMode is set to GrowAndShrink. In the MakeGrid method, the grid labels are now part of the panel (panel1) rather than the form (this).
Since we will need to know the number of rows and columns in many more places, they have been made private attributes of the application (numRows and numColumns, respectively). All of the loops have been modified to use these attributes instead of grid.GetUpperBound(). The values of these attributes are obtained by the MakeGrid method from the rowSize and colSize textboxes instead of being passed in as arguments.
Assignment
(10 points) We will do the following in class. Details will be given during class. The corresponding chapters of the textbook are given in parentheses.
Add a MenuStrip control to the program. For this program, we will create a "File" menu with three drop-down items, "Open", "Save", and "Save As". (Chapter 21)
Add a handler for the Open menu item. This handler should ask the user for the name of a file to open using a OpenFileDialog object, then read the configuration out of the file using a StreamReader object, and set the current grid to the configuration.
Add a handler for the Save As menu item. This handler basically does the opposite of Open. It asks the user for the name for the saved configuration file using a SaveFileDialog object (Chapter 21, p. 385-386), then writes out the configuration from the grid using a StreamWriter object (Chapter 21, p. 382) in the following format: the first line is the number of rows, the second line is the number of columns, the rest of the file is the grid configuration as lines of spaces and asterisks.
Add a handler for the Save menu item. This handler will save the configuration to the most recent file name given. If there is no current file name, it behaves like Save As.
04/01/08