Home

Visual C# Examples:
Red Oak High School - Grades

Introduction

This exercise is meant to apply the theories reviewed in file processing. It simulates taking the grades of a student and saving them to a file. It also provides the ability to open a previously saved record of a student. For this exercise, we use the FileStream class.

 

 

Practical LearningPractical Learning: Introducing File Streaming

  1. Create a new Windows Forms Application named ROSH1
  2. Design the form as follows:
     
    Form Design
    Control Name Text Other Properties
    GroupBox GroupBox   Identification  
    Label Label   Student Name:  
    TextBox txtStudentName    
    Label   School Year:  
    TextBox txtSchoolYear1    
    Label   /  
    TextBox txtSchoolYear2    
    GroupBox   Grades  
    Label   English:  
    TextBox txtEnglish 0.00 TextAlign: Right
    Label   History:  
    TextBox txtHistory 0.00 TextAlign: Right
    Label   Economics:  
    TextBox txtEconomics 0.00 TextAlign: Right
    Label   2nd Language:  
    TextBox txt2ndLanguage 0.00 TextAlign: Right
    Label   Geography:  
    TextBox txtGeography 0.00 TextAlign: Right
    Label   Arts:  
    TextBox txtArts 0.00 TextAlign: Right
    Label   Math:  
    TextBox txtMath 0.00 TextAlign: Right
    Label   Science:  
    TextBox txtScience 0.00 TextAlign: Right
    Label   Phys Educ:  
    TextBox txtPhysEduc 0.00 TextAlign: Right
    GroupBox   Grade Processing  
    Button btnCalculate Calculate  
    Label   Total:  
    TextBox txtTotal 0.00 TextAlign: Right
    Label   Average:  
    TextBox txtAverage 0.00 TextAlign: Right
    Button btnSave Save  
    Button btnOpen Open  
    Button btnClose Close  
  3. Double-click the Calculate button and implement its Click event as follows:
     
    private void btnCalculate_Click(object sender, System.EventArgs e)
    {
    	 decimal english = 0, history = 0, economics = 0, language2 = 0,
    		     geography = 0, arts = 0, math = 0, science = 0,
    		     physEduc = 0, total = 0, average = 0;
    
    	 // Retrieve the value entered for each course
    	 try {
    		 english = decimal.Parse(this.txtEnglish.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the English course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    
    	 try {
    		 history = decimal.Parse(this.txtHistory.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the History course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    			 
    	 try {
    		 economics = decimal.Parse(this.txtEconomics.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the Economics course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    			 
    	 try {
    		 language2 = decimal.Parse(this.txt2ndLanguage.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the 2nd Language course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    			 
    	 try {
    		 geography = decimal.Parse(this.txtGeography.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the Geography course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    			 
    	 try {
    		 arts = decimal.Parse(this.txtArts.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the Arts course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    			 
    	 try {
    		 math = decimal.Parse(this.txtMath.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the Math course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    			 
    	 try {
    		 science = decimal.Parse(this.txtScience.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the Science course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    			 
    	 try {
    		 physEduc = decimal.Parse(this.txtPhysEduc.Text);
    	 }
    	 catch(FormatException )
    	 {
    		 MessageBox.Show("The value you entered for the Physical Education course is not valid");
    		 this.txtEnglish.Focus();
    	 }
    
    	 // Calculate the total and the average
    	 total = english + history + economics + language2 +
    		     geography + arts + math + science + physEduc;
    	 average = total / 9;
    
    	 // Display the total and the average
    	 this.txtTotal.Text = total.ToString("F");
    	 this.txtAverage.Text = average.ToString("F");
    }
  4. Return to the form.
    Double-click the Close button and implement its event as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
  5. Execute the application to test it
  6. Right-click the form and click View Code
  7. In the top section of the file, add the using System. IO; line
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    
    namespace ROSH1
    {
  8. Display the form
    In the Toolbox, click the SaveFileDialog button SaveFileDialog and click the form
  9. In the Properties window, change the following properties
    DefaultExt: ros
    Filter: ROSH Students Grades (*.ros)|*.ros|All Files(*.*)|
    Title: Save Student Grades As
  10. On the form, double-click the Save button and implement its Click event as follows:
     
    private void btnSave_Click(object sender, System.EventArgs e)
    {			
    	 FileStream   stmGrades = null;
    	 BinaryWriter bnwGrades = null;
    
    	 // The information will not be saved if there is no name for the student
    	 if( this.txtStudentName.Text.Equals("") )
    	 {
    		 MessageBox.Show("The report cannot be saved without the name of the student");
    		 this.txtStudentName.Focus();
    		 return;
    	 }
    	 // The information will not be saved if there is no school year
    	 if( this.txtSchoolYear1.Text.Equals("") )
    	 {
    		 MessageBox.Show("The report cannot be saved without the school year");
    		 this.txtSchoolYear1.Focus();
    		 return;
    	 }
    	 if( this.txtSchoolYear2.Text.Equals("") )
    	 {
    		 MessageBox.Show("The report cannot be saved without the school year");
    		 this.txtSchoolYear2.Focus();
    		 return;
    	 }
    
    	 string strFilename = this.txtStudentName.Text +
    		 this.txtSchoolYear1.Text + this.txtSchoolYear2.Text;
    	 this.saveFileDialog1.FileName = strFilename;
    	 // Display the Save dialog box
    	 // Find out if the user clicked OK or pressed Enter
    	 if( this.saveFileDialog1.ShowDialog() == DialogResult.OK )
    	 {
    		 // If the used provided a name that already exists for an existing file
    		 if( File.Exists(this.saveFileDialog1.FileName) )
    		 {
    			 // Find out if the user wants to replace the existing file
    System.Windows.Forms.DialogResult answer = MessageBox.Show(string.Concat("The student report exists already.",
    					                                                   "Do you want to replace it?"),
    								   "Save Student Report",
    								   MessageBoxButtons.YesNo);
    
    			 // If the user wants to replace it
    			 if( answer == DialogResult.Yes )
    			 {
    				 stmGrades = new FileStream(this.saveFileDialog1.FileName, FileMode.Create);
    				 bnwGrades = new BinaryWriter(stmGrades);
    
    				 // Then overwrite it
    				 bnwGrades.Write(this.txtStudentName.Text);
    				 bnwGrades.Write(this.txtSchoolYear1.Text);
    				 bnwGrades.Write(this.txtSchoolYear2.Text);
    				 bnwGrades.Write(decimal.Parse(this.txtEnglish.Text));
    				 bnwGrades.Write(decimal.Parse(this.txtHistory.Text));
    				 bnwGrades.Write(decimal.Parse(this.txtEconomics.Text));
    				 bnwGrades.Write(decimal.Parse(this.txt2ndLanguage.Text));
    				 bnwGrades.Write(decimal.Parse(this.txtGeography.Text));
    				 bnwGrades.Write(decimal.Parse(this.txtArts.Text));
    				 bnwGrades.Write(decimal.Parse(this.txtMath.Text));
    				 bnwGrades.Write(decimal.Parse(this.txtScience.Text));
    				 bnwGrades.Write(decimal.Parse(this.txtPhysEduc.Text));
    
    				bnwGrades.Close();
    				stmGrades.Close();
    			}
    		}
    		else
    		{	
    			stmGrades = new FileStream(this.saveFileDialog1.FileName, FileMode.Create);
    			bnwGrades = new BinaryWriter(stmGrades);
    						
    			bnwGrades.Write(this.txtStudentName.Text);
    			bnwGrades.Write(this.txtSchoolYear1.Text);
    			bnwGrades.Write(this.txtSchoolYear2.Text);
    			bnwGrades.Write(decimal.Parse(this.txtEnglish.Text));
    			bnwGrades.Write(decimal.Parse(this.txtHistory.Text));
    			bnwGrades.Write(decimal.Parse(this.txtEconomics.Text));
    			bnwGrades.Write(decimal.Parse(this.txt2ndLanguage.Text));
    			bnwGrades.Write(decimal.Parse(this.txtGeography.Text));
    			bnwGrades.Write(decimal.Parse(this.txtArts.Text));
    			bnwGrades.Write(decimal.Parse(this.txtMath.Text));
    			bnwGrades.Write(decimal.Parse(this.txtScience.Text));
    			bnwGrades.Write(decimal.Parse(this.txtPhysEduc.Text));
    					
    			bnwGrades.Close();
    			stmGrades.Close();
    		}
    	}
    }
  11. Execute the application and create a record:
     
  12. Save the record and close the form
  13. In the Toolbox, click the OpenFileDialog button SaveFileDialog and click the form
  14. In the Properties window, change the following properties
    DefaultExt: ros
    Filter: ROSH Students Grades (*.ros)|*.ros|All Files(*.*)|
    Title: Open Student Grades
  15. On the form, double-click the Save button and implement its Click event as follows:
     
    private void btnOpen_Click(object sender, System.EventArgs e)
    {
    	 // Display the Open dialog box
    	 // Find out if the user selected a file and clicked OK
    	 if( this.openFileDialog1.ShowDialog() == DialogResult.OK )
    	 {
    	 	FileStream   stmGrades = new FileStream(this.openFileDialog1.FileName, FileMode.Open);
    		 BinaryReader bnrGrades = new BinaryReader(stmGrades);
    
    		 if( File.Exists(this.openFileDialog1.FileName) )
    		 {
    			 // Retrieve the values from the file
    			 this.txtStudentName.Text = bnrGrades.ReadString();
    			 this.txtSchoolYear1.Text = bnrGrades.ReadString();
    			 this.txtSchoolYear2.Text = bnrGrades.ReadString();
    			 this.txtEnglish.Text     = bnrGrades.ReadDecimal().ToString("F");
    			 this.txtHistory.Text     = bnrGrades.ReadDecimal().ToString("F");
    			 this.txtEconomics.Text   = bnrGrades.ReadDecimal().ToString("F");
    			 this.txt2ndLanguage.Text = bnrGrades.ReadDecimal().ToString("F");
    			 this.txtGeography.Text   = bnrGrades.ReadDecimal().ToString("F");
    			 this.txtArts.Text        = bnrGrades.ReadDecimal().ToString("F");
    			 this.txtMath.Text        = bnrGrades.ReadDecimal().ToString("F");
    			 this.txtScience.Text     = bnrGrades.ReadDecimal().ToString("F");
    			 this.txtPhysEduc.Text    = bnrGrades.ReadDecimal().ToString("F");
    			 
    			 // Close the stream
    			 bnrGrades.Close();
    			 stmGrades.Close();
    
    			 // Since we didn't save the results of the calculations,
    			 // call the Calculate button to do it
    			 this.btnCalculate_Click(sender, e);
    		 }
    	 }
    }
  16. Save all
 

Home Copyright © 2004-2010 FunctionX, Inc.