【发布时间】:2020-08-26 12:42:41
【问题描述】:
所以我一直在试图用保存在文本文件中的 ID 号填充此列表框。从文件中取出行并将它们放入这样的列表框中很简单,但是该 ID 号也与我想在其他文本框中显示的更多信息共享。
编辑 所以这是一个逗号分隔的文件,下面是一个学生存储信息的例子:
1234561, Hubert, Huphrey, 123 Apple, Townsville, Some State, MM/DD/YYYY
说白了,我有这个包含多个 ID #、名称和地址的文本文件,但我只想显示 ID #,以便可以从列表框中单击它们,以便其余的的信息可以显示在屏幕上。
我已经接受了很多建议,但我的脑袋却想不出几个关键点。让我先分享一下我目前拥有的代码:
编辑我已经更新了使用新的Student 类的方法。但是现在我需要弄清楚如何使用集合来获取类的属性。
List<Student> colStudents = new List<Student>();
public MainWindow()
{
InitializeComponent();
}
private void btnGetStudents_Click(object sender, RoutedEventArgs e)
{
try
{
//Define a Student object variable with the name StudentInfo
string strStudentList;
// Declare a StreamReader variable for Student Object
StreamReader srStudent = new StreamReader("StudentData.txt");
// intIndex is the index of the contact chosen
int intIndex = lstStudentsID.SelectedIndex;
//Clear list box to avoid having it fill up too much
lstStudentsID.Items.Clear();
// Read the disk file structure
while (srStudent.EndOfStream == false)
{
// Read disk file into a string variable using the ReadLine method.
strStudentList = srStudent.ReadLine();
// Tokenize the string read from Student object
string[] tokenize = strStudentList.Split(',');
// Create a student object instance from the Student class.
Student StudentInfo = new Student();
// Set properties of Student object to array element containing the student data
StudentInfo.StudentID = int.Parse(tokenize[0]);
StudentInfo.StudentFirstName = tokenize[1];
StudentInfo.StudentLastName = tokenize[2];
StudentInfo.StudentAddressCity = tokenize[3];
StudentInfo.StudnetBirthDate = tokenize[4];
// Add student object to collection
colStudents.Add(StudentInfo);
// Add ID's to the listbox
lstStudentsID.Items.Add(StudentInfo.StudentID);
}
//Close disk file after all records have been read in
srStudent.Close();
}
catch(Exception ex)
{
MessageBox.Show("Experiencing the following disk problems: " + Environment.NewLine + ex.Message, "Disk File", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
编辑: 现在我的 while 循环运行良好,我需要弄清楚如何利用该集合,或者只是想办法使用第二种和第三种方法打印出所选学生的全名和地址
这是我的新课程:
public class Student
{
// Properties
private double _ID; // Student's ID, double just in case int is too small
private string _StudentFirstName; // Student's Name
private string _StudentLastName;
private string _StudentAddressStreet;
private string _StudentAddressCity;
private string _StudentBirthDate;
// Constructor
public Student()
{
_ID = 0;
_StudentFirstName = "";
_StudentLastName = "";
_StudentAddressStreet = "";
_StudentAddressCity = "";
_StudentBirthDate = "";
}
// ID Property
public double StudentID
{
get { return _ID; }
set { _ID = value; }
}
// First Name Property
public string StudentFirstName
{
get { return _StudentFirstName; }
set { _StudentFirstName = value; }
}
// Last name Property
public string StudentLastName
{
get { return _StudentLastName; }
set { _StudentLastName = value; }
}
// Address Property
public string StudentAddressStreet
{
get { return _StudentAddressStreet; }
set { _StudentAddressStreet = value; }
}
// Address Property
public string StudentAddressCity
{
get { return _StudentAddressCity; }
set { _StudentAddressCity = value; }
}
// Address Property
public string StudnetBirthDate
{
get { return _StudentBirthDate; }
set { _StudentBirthDate = value; }
}
}
【问题讨论】:
-
当您单击该按钮时,您的程序应该打开并读取磁盘文件。当从磁盘文件中读取记录时,您应该创建一个学生对象,更新学生对象的属性,然后将其添加到集合中 (colStudents)。它还应该将学生 ID 添加到列表框控件。当用户点击 ListBox 控件中的学生 id 时,将从学生集合中检索学生对象,以便您可以显示窗口的学生信息。
-
Student类在哪里?具有 ID、姓名和地址属性的学生班级似乎是缺失的部分。public class Student { public int ID { get; set; } public string Name { get; set; } public string Addesss { get; set; }。然后在读取文本文件之前创建一个List<Student>。对于每个ReadLine,创建一个新的Student对象,然后将其添加到List<Student>。当前代码没有“保存”从文件中读取的任何数据。 -
当然!我应该知道那是我所缺少的!所以基本上我可以更改我在该领域的列表以使用该类!
-
@JohnG 所以属性将是类中的字段,对吧?会不会是这样的:
// ID Property public double StudentID { get { return _ID; } set { _ID = value; } } -
正确,我的最后一条评论显示了一个简单的
Student类的代码。
标签: c# arraylist text-files streamreader