码迷,mamicode.com
首页 > Windows程序 > 详细

C#大学课程(第五版)课后习题17.3学生成绩文件

时间:2018-04-07 20:08:33      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:private   classname   invalid   open   ogre   while   als   windows   MF   

/*17.3
(学生成绩文件) 创建一个程序,它将学生的成绩保存到一一个文本文件中。这个文件应当包含每位学生的姓名、ID 号、课程以及成绩信息。应允许用户载人文件并以只读文本框模式显示它的内容。显示信息时应具有如下的格式:
LastNane,FirstName: ID# Class Grade
一些数据样本如下:
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( "You 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#大学课程(第五版)课后习题17.3学生成绩文件

标签:private   classname   invalid   open   ogre   while   als   windows   MF   

原文地址:https://www.cnblogs.com/hsyv123ve/p/8734183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!