![]() |
Visual C# Examples: |
![]() |
|
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.
|
|
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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");
}
|
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace ROSH1
{
|
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();
}
}
}
|

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);
}
}
}
|
|
|
||
| Home | Copyright © 2004-2010 FunctionX, Inc. | |
|
|
||