下面是代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace GPA_Calculator
{
public partial class Retrieve : Form
{
//Declare Global vatiables const char DELIM = '*';//declare delimiter between data fields
const string FILENAME = @"GPA.txt";//declare location of data file
string name, nameEntered;//declare user name and the name user entered
string recordIn;// declare record read from data file
string[] fields;//declare array of fields for each record
string adminNO, adminNOEntered;//declare the adminNO
string moduleName, moduleNO, grade;
double moduleCredit;
int lgFailure; //declare the login failure times FileStream outFile;//file stream objected
StreamReader reader;//Stream reader objected
public Retrieve()
{
InitializeComponent();
} private void retrieveBtn_Click(object sender, EventArgs e)
{
bool success = false;
// Create a file stream object to open a file called FILENAME
outFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
// Create a stream reader with the outFile File Stream object
reader = new StreamReader(outFile); try
{
do
{
// Use stream reader to read the file
recordIn = reader.ReadLine();
//Split up a string using the delimter and
//store them to an array called field
fields = recordIn.Split(DELIM); // Pick the content from the array
nameEntered = nameTB.Text;
adminNOEntered = adminTB.Text;
name = fields[0];
adminNO = fields[1];
moduleName = fields[2];
moduleNO = fields[3];
moduleCredit = Convert.ToDouble(fields[4]);
grade = fields[5]; //Check the if the name and pin entered is correct
if (nameEntered == name && adminNOEntered == adminNO)
{
MessageBox.Show("Information is found!"); // Login successfully,break
success = true;
break;
}
} while (true);
//show the result in the label
showLbl.Text = "Student's Name: " + name + Environment.NewLine.ToString()+"Admin NO.: " + adminNO +
Environment.NewLine.ToString()+"Module Name: "
+ moduleName + Environment.NewLine.ToString()+ "Module NO.: " + moduleNO
+ Environment.NewLine.ToString() + "Grade is: " + grade; }
catch (Exception)
{
// Login fail,login failure time + 1
MessageBox.Show("Please check your name and admin NO.!");
lgFailure++; //clear the text boxes
nameTB.Clear();
adminTB.Clear();
}
//close the reader and the outfile
reader.Close();
outFile.Close(); // if login failure time>=3, form closed
if (lgFailure >= 3)
{
MessageBox.Show("You have exceeded the number of tries, bye!!"); this.Close();
}
} private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
}
}程序的要求是从file里面读出所有的一个学生的成绩记录 比如 这个文本文件里面有John*110038A*PMP*EG1745*4*B
Lucy*110042A*Data communacation*EG1755*4*A
John*110038A*Infocomm System*EG1756*4*B
Jim*110056A*Graphic Design*4*A点击按钮 Retrieve 输入姓名和学号 就要把所有关于这个学生的信息显示在一个label上比如我输入 John 110038A 就要显示
Name:John AdminNO.110038A
PMP EG1745 4 B
Infocomm System EG1756 4 B
我现在做的是只能读一行数据, 我想知道怎么把全部数据都读出来大侠们帮帮忙了 小弟不胜感激!!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace GPA_Calculator
{
public partial class Retrieve : Form
{
//Declare Global vatiables const char DELIM = '*';//declare delimiter between data fields
const string FILENAME = @"GPA.txt";//declare location of data file
string name, nameEntered;//declare user name and the name user entered
string recordIn;// declare record read from data file
string[] fields;//declare array of fields for each record
string adminNO, adminNOEntered;//declare the adminNO
string moduleName, moduleNO, grade;
double moduleCredit;
int lgFailure; //declare the login failure times FileStream outFile;//file stream objected
StreamReader reader;//Stream reader objected
public Retrieve()
{
InitializeComponent();
} private void retrieveBtn_Click(object sender, EventArgs e)
{
bool success = false;
// Create a file stream object to open a file called FILENAME
outFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
// Create a stream reader with the outFile File Stream object
reader = new StreamReader(outFile); try
{
do
{
// Use stream reader to read the file
recordIn = reader.ReadLine();
//Split up a string using the delimter and
//store them to an array called field
fields = recordIn.Split(DELIM); // Pick the content from the array
nameEntered = nameTB.Text;
adminNOEntered = adminTB.Text;
name = fields[0];
adminNO = fields[1];
moduleName = fields[2];
moduleNO = fields[3];
moduleCredit = Convert.ToDouble(fields[4]);
grade = fields[5]; //Check the if the name and pin entered is correct
if (nameEntered == name && adminNOEntered == adminNO)
{
MessageBox.Show("Information is found!"); // Login successfully,break
success = true;
break;
}
} while (true);
//show the result in the label
showLbl.Text = "Student's Name: " + name + Environment.NewLine.ToString()+"Admin NO.: " + adminNO +
Environment.NewLine.ToString()+"Module Name: "
+ moduleName + Environment.NewLine.ToString()+ "Module NO.: " + moduleNO
+ Environment.NewLine.ToString() + "Grade is: " + grade; }
catch (Exception)
{
// Login fail,login failure time + 1
MessageBox.Show("Please check your name and admin NO.!");
lgFailure++; //clear the text boxes
nameTB.Clear();
adminTB.Clear();
}
//close the reader and the outfile
reader.Close();
outFile.Close(); // if login failure time>=3, form closed
if (lgFailure >= 3)
{
MessageBox.Show("You have exceeded the number of tries, bye!!"); this.Close();
}
} private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
}
}程序的要求是从file里面读出所有的一个学生的成绩记录 比如 这个文本文件里面有John*110038A*PMP*EG1745*4*B
Lucy*110042A*Data communacation*EG1755*4*A
John*110038A*Infocomm System*EG1756*4*B
Jim*110056A*Graphic Design*4*A点击按钮 Retrieve 输入姓名和学号 就要把所有关于这个学生的信息显示在一个label上比如我输入 John 110038A 就要显示
Name:John AdminNO.110038A
PMP EG1745 4 B
Infocomm System EG1756 4 B
我现在做的是只能读一行数据, 我想知道怎么把全部数据都读出来大侠们帮帮忙了 小弟不胜感激!!
我是刚学习C#,有好多东西都很陌生。楼上大侠,拜托能给个例子吗?
if (nameEntered == name && adminNOEntered == adminNO)
{
MessageBox.Show("Information is found!"); // Login successfully,break
success = true;
break;
}
如:
name = fields[0];
adminNO = fields[1];
moduleName = fields[2];
moduleNO = fields[3];
moduleCredit = Convert.ToDouble(fields[4]);
grade = fields[5];
==>
List<Student> list=new List<Student>();//注:这个要放在循环外面
Student stu=new Student(); //这个要放在循环里面
stu.name = fields[0];
stu.adminNO = fields[1];
stu.moduleName = fields[2];
stu.moduleNO = fields[3];
stu.moduleCredit = Convert.ToDouble(fields[4]);
stu.grade = fields[5];
list.Add(stu);
再显示数据的时候想显示哪条就显示哪条