/*17.3
(Student achievement file) creates a program that saves students ' scores to one by one text files. This file should contain each student's name, ID number, course, and score information. The user should be allowed to manned the file and display its contents in a read-only text box mode. You should have the following format when displaying information:
lastnane,firstname:id# Class Grade
Some data samples are as follows:
Jones,bob:1 "Introduction to Computer Science" "A-"
Johnson,sarah:2 "Data Structures" "B +"
Smith,sam:3 "Data Structures" "C"
*/
Using System;
Using System.Windows.Forms;
Using System.IO;
Namespace Grades
{
public partial class Gradesform:form
{
Private StreamWriter output;
private StreamReader input;
private bool saved = false;
Public Gradesform ()
{
InitializeComponent ();
}
private void Alltextbox_textchanged (object sender, EventArgs e)
{
Enableenterbutton ();
}
private void Saveasbutton_click (object sender, EventArgs e)
{
DialogResult result;
String FileName;
using (SaveFileDialog chooser = new SaveFileDialog ())
{
result = Chooser. ShowDialog ();
FileName = Chooser. FileName;
}
if (result = = DialogResult.OK)
{
if (FileName = = string. Empty)
MessageBox.Show ("entered an invalid file name.",
"Invalid File name", MessageBoxButtons.OK,
MessageBoxIcon.Information);
Else
{
Output = new StreamWriter (fileName);
saveasbutton.enabled = false;
Saved = true;
}
Enableenterbutton ();
}
}
private void Enableenterbutton ()
{
if (lasttextbox.text! = string. Empty &&
Firsttextbox.text! = string. Empty &&
Idtextbox.text! = string. Empty &&
Classtextbox.text! = string. Empty &&
Gradetextbox.text! = string. Empty && Saved)
Enterbutton.enabled = true;
Else
enterbutton.enabled = false;
}
private void Enterbutton_click (object sender, EventArgs e)
{
string last = Lasttextbox.text;
string first = Firsttextbox.text;
string id = idtextbox.text;
string className = Classtextbox.text;
string grade = Gradetextbox.text;
Output. WriteLine (last + "\ T" + first + "\ T" + ID +
"\ t" + ClassName + "\ T" + grade);
Statuslabel.text = "Entry saved";
Lasttextbox.clear ();
Firsttextbox.clear ();
Idtextbox.clear ();
Classtextbox.clear ();
Gradetextbox.clear ();
}
private void Loadbutton_click (object sender, EventArgs e)
{
if (output! = NULL)
{
Output. Close ();
output = null;
Statuslabel.text = "Closing file";
Saveasbutton.enabled = true;
Saved = false;
}
DialogResult result;
String FileName;
using (OpenFileDialog chooser = new OpenFileDialog ())
{
result = Chooser. ShowDialog ();
FileName = Chooser. FileName;
}
if (result = = DialogResult.OK)
{
if (FileName = = string. Empty)
MessageBox.Show ("Invalid file name", "Invalid file name",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Else
{
input = new StreamReader (fileName);
String entry = input. ReadLine ();
Gradestextbox.clear ();
while (entry! = NULL)
{
Gradestextbox.appendtext (Formatentry (entry) +
"\ r \ n");
Entry = input. ReadLine ();
}
Statuslabel.text = "File loaded";
}
}
}
private string Formatentry (String fromfile)
{
Char[] splitters = {' \ t '};
string[] data = fromfile.split (splitters);
string result = data[0] + "," + data[1] +
": \ T" + data[2] + "\ T" + data[3] + "\ T" + data[4];
return result;
}
}
}
C # University Course (fifth edition) after class exercise 17.3 student Achievement Document