简体   繁体   English

使用ac#Windows窗体中的按钮逐行将.txt文件读入多个文本框

[英]Reading a .txt file line by line into multiple text boxes with buttons in a c# windows form

I have got a txt file with 10 lines in it, each line is a record with 5 different fields; 我有一个txt文件,其中有10行,每行是一个包含5个不同字段的记录; Farrell,Jade,Louise,2011/09/13,F 法雷尔,玉,路易丝,2011/09/13,F

I am using the commas to split record by FamilyName, FirstName, MiddleName, EnrolmentDate and Gender. 我正在使用逗号按FamilyName,FirstName,MiddleName,EnrolmentDate和Gender拆分记录。 I want each field to have its own text box then use buttons to look through the different records. 我希望每个字段都有其自己的文本框,然后使用按钮浏览不同的记录。

Everything so far is working under the load button which reads the data from the file and puts it into the text boxes using the code below which works but it only shows the first record so i want a buttons to show the next record, previous record, first and last record and also a button to sort the data from AZ by the family name. 到目前为止,所有内容都在“加载”按钮下工作,该按钮使用以下代码从文件中读取数据并将其放入文本框,该代码可以正常工作,但只显示第一个记录,因此我希望按钮显示下一个记录,上一个记录,第一个和最后一个记录,以及一个按姓氏对来自AZ的数据进行排序的按钮。 Any help on how to go forward would be great! 任何关于如何前进的帮助都将是巨大的! thanks! 谢谢!

private void Load_BT_Click(object sender, EventArgs e)        
{
    OpenFileDialog filechooser = new OpenFileDialog();
    StreamReader filereader = new StreamReader("StudentFile.txt");  
    String inputrecord = filereader.ReadLine();
    string[] inputfields;

    if (inputrecord != null)
    {
        inputfields = inputrecord.Split(',');
        FamName_TXT.Text = inputfields[0];
        FirstName_TXT.Text = inputfields[1];
        MiddleName_TXT.Text = inputfields[2];
        Enrolment_txt.Text = inputfields[3];
        Gender_TXT.Text = inputfields[4];
    }
    else
    {
        MessageBox.Show("End of File");
    }         
}

I think there are some design issues here, but i will address your immediate concern. 我认为这里存在一些设计问题,但是我将解决您的直接关注。 The problem is you only read one line. 问题是您只能读一行。 You need to iterate over all the lines in the textfile. 您需要遍历文本文件中的所有行。 I am assuming you want the load button to load all the data at once. 我假设您希望加载按钮一次加载所有数据。

string[] allRecords = filereader.ReadAllLines();
foreach(string inputRecord in allRecords) {
    string[] inputfields = inputRecord.Split(',');
    //insert the textbox.Text += inputfields[0] + "\n"; etc
}

if you want a single button to resort the data across all the textboxes. 如果您希望使用一个按钮来遍历所有文本框的数据。

  1. you really should create a class called Person with properties that correspond to your fields and override compareTo so you can sort by last name or maybe use linq to do the sort for you. 您确实应该创建一个名为Person的类,该类具有与您的字段相对应的属性,并覆盖compareTo,以便您可以按姓氏排序,也可以使用linq为您进行排序。
  2. you need a list that will host all of these person objects 您需要一个包含所有这些人对象的列表
  3. from there you can populate the textboxes accordingly 从那里您可以相应地填充文本框
  4. Create a sort button that will reorder the list or create a new list and repopulate the textboxes. 创建一个排序按钮,它将对列表重新排序或创建一个新列表并重新填充文本框。

1-3 would be done in the load button. 1-3将在加载按钮中完成。 The reason for the person class is because you want all the data you read in from the file to be associate with an object. 使用人员类的原因是因为您希望将从文件中读取的所有数据与对象相关联。 if you try to do the sorting directly from the textboxes as you seem to be trying to do you will run into issues such as how to make sure the data doesnt get jumbled. 如果您试图像尝试做的那样直接从文本框中进行排序,则会遇到诸如如何确保数据不会混乱的问题。 It certainly may be possible but it is not an elegant way and would be more work in my mind 当然有可能,但是这不是一种优雅的方式,在我看来会做得更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM