简体   繁体   English

从行数未知的文件(c#)中读取文本并将行添加到预定义标签

[英]Read text from file (c#) of unknown number of lines and add lines to predefined labels

I am currently working on a windows form application. 我目前正在Windows窗体应用程序上工作。 I need to read text from a particular file, i am not sure of the number of lines that will be in this text file at a given time but i need to retrieve each line and display them on a form page. 我需要从特定文件中读取文本,我不确定给定时间该文本文件中的行数,但是我需要检索每一行并将其显示在表单页面上。 i have tried to store the string in an array but i get an error "Make sure that the maximum index on a list is less than the list size". 我试图将字符串存储在数组中,但出现错误“确保列表上的最大索引小于列表大小”。

Also note that i have created some labels on certain positions for each lines, (i made about ten labels). 还要注意,我已经在每行的某些位置上创建了一些标签(我制作了大约十个标签)。 This is the portion of the code giving me problem: 这是代码中给我带来问题的部分:

            string[] edfTaskList = System.IO.File.ReadAllLines(edfTaskfile);
            oldtask1.Text = edfTaskList[0];
            oldtask2.Text = edfTaskList[1];
            oldtask3.Text = edfTaskList[2];
            oldtask4.Text = edfTaskList[3];
            oldtask5.Text = edfTaskList[4];
            oldtask6.Text = edfTaskList[5];
            oldtask7.Text = edfTaskList[6];
            oldtask8.Text = edfTaskList[7];
            oldtask9.Text = edfTaskList[8];
            oldtask10.Text = edfTaskList[9];

            oldTaskPanel.Controls.Add(oldtask1);
            oldTaskPanel.Controls.Add(oldtask2);
            oldTaskPanel.Controls.Add(oldtask3);
            oldTaskPanel.Controls.Add(oldtask4);
            oldTaskPanel.Controls.Add(oldtask5);
            oldTaskPanel.Controls.Add(oldtask6);
            oldTaskPanel.Controls.Add(oldtask7);
            oldTaskPanel.Controls.Add(oldtask8);
            oldTaskPanel.Controls.Add(oldtask9);
            oldTaskPanel.Controls.Add(oldtask10);

I would do something like this: 我会做这样的事情:

private int lastLablePos;

    public Form1()
    {
        InitializeComponent();
        lastLablePos = panel1.Location.Y;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] lines = System.IO.File.ReadAllLines(@"D:\test.txt");
        Label[] labels = new Label[lines.Length];

        for (int i = 0; i < lines.Length; i++)
        {

            labels[i] = new Label();
            labels[i].Text = lines[i];
        }

        foreach (Label lable in labels)
        {
            lable.Location = new Point(0, lastLablePos);
            lable.AutoSize = true;
            panel1.Controls.Add(lable);
            lastLablePos += 30;
        }
    }

and finally add those labels to form with coordinates etc. 最后将这些标签添加到带有坐标等的表格中

Are you sure that the array totally got 10 or more entries? 您确定阵列总共有10个或更多条目吗? I guess that this is the problem, you are addressing more values than you actually have. 我想这就是问题所在,您正在解决的价值超出了实际。

Either loop through your entries (by using a foreach loop) and create your labels dynamically or use other control elements as Saverio Terracciano mentioned. 可以遍历您的条目(使用foreach循环)并动态创建标签,也可以使用Saverio Terracciano提到的其他控制元素。

使用File.ReadLines代替File.ReadAllLines并使用DataGrid显示数据,请参见File.ReadLines

There are several different approaches you could use to solve your problem, which is rather broad, assuming you have no way to have an upperbound on the number of lines you could: 可以使用几种不同的方法来解决问题,这很广泛,假设您没有办法限制行数的上限:

  • Build and add dynamically the controls you need to show the lines you read in your file 动态构建并添加所需的控件,以显示在文件中读取的行
  • Use a different kind of control like a ListBox or a DataGrid 使用其他类型的控件,例如ListBox或DataGrid

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

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