Assignment-11 Solution


Checkbox control
Saving to the text file
Reading from a text file
Form Closing Event Method
Form_Load event method

Problem Description: Design a C# project to store up to 100 students’ information in parallel arrays. The user will enter each student’s name, score in provided Textboxes and check the Checkbox if student is a CIT major, followed by a click on the Enter button to store the student in the parallel arrays.

Figure 1 - Screen capture when Display is clicked

Class Scope Declarations:

Declare a named constant to store size of the parallel arrays = 100.
Declare 3 parallel arrays to store up to 100 students’ names, scores and CIT major (true / false). Use the named constant at declaration.
Declare a counter to keep track of the next available index in the array.
Declare a variable to store the file name with the full path:
Private String mFileName = Path.Combine (Application.StartupPath , “Students.txt”);

Enter Button (pseudo-code)

Validate the user’s input - Call the helper method.
If data is not valid - return.
Store the entered data in parallel arrays.If the check box is checked, store true in the array, otherwise store false.
If array is fullInform the user
Disable the Enter button.
Call the helper method to clear the input.

Display Button:

Inform the user if array is empty, and return.
Display all the names, scores and CIT major (true / false) in the Listbox.

Stats Button:

Inform the user if array is empty, and return.
Clear the Listbox
Display number of students entered.
Display number of CIT students entered.
Compute the average. Display it with proper format.

Clear Button:

Call ClearInput() to clear the entered input.
Clear the Listbox.

Exit Button: End program execution.

FormClosing Event:

Save data stored in parallel arrays to a text file named students.txt, one student per line, with tab delimited fields. Refer to Figure-3.

Declare an object variable of type StreamWriter.
Open the file txt at the same location as your project’s .exe
Write a loop to write each student’s data on a separate line, tab delimited.
Close the file.

Helper Methods:

ValidateInput():Input: none
Output data type: bool
Task: Validate user’s input based on the following rules:
- Existence check on name.

- Score must be a whole number.

- Valid range for score is 0 to 100, both inclusive.

ClearInput ( )Input: none
Output: void
Task: Clear the entered data and set the focus to the top textbox.
Call this method at the end of the Enter button and from the Clear button.

DisplayMessage( )
Input: String
Output data type: void
Task: Display the input string using MessageBox.Show(). The Messagebox should include all 4 arguments.

FormClosing Event Method

Save content of the parallel arrays to the text file in this event procedure.

Confirm with the user whether to end the program execution or not.Use MessageBox.Show to ask this question. Provide buttons Yes and No in the Messagebox plus the Question icon.
If the user’s answer is NO,Cancel the closing event.

Save data stored in parallel arrays to a text file.Declare an object variable of StreamWriter Open the file (use class scope filename) to write to.
Write a loop to write each student’s data to the file in a tab delimited format.
Close the file.
Include Exception Handling code to handle exceptions (try – catch - finally).

Figure 22 - An image of the text file

Form_Load Event Method

Upload / read data from a text file into the program in parallel arrays in this event method.

Check if the file exists (Use the class scope filename).If file does not exist, return.
Declare an Object variable of StreamReader class and open the file mFilename for reading.
Declare a string variable to store a line read from the file.
Write a loop to read until end of the file.Read a line into the string variable.
Split the line to extract pieces of data.
Store pieces of data in parallel arrays at mIndex. -
Close the file.
Include Exception Handling code to handle exceptions (try – catch - finally).
Powered by