繁体   English   中英

如何使用从文本文件中读取的项目填充列表框

[英]How do you populate a ListBox with items read from a text file

所以我尝试读取该文件并确认它有效,然后尝试将内容添加到列表框但它不起作用。 我这样做的方法是首先读取文件,逐行拆分文件并将内容放入字符串数组,然后使用 for 循环将内容添加到列表框。 起初我的问题是列表框没有添加内容,然后发生了一些变化,现在看起来文件没有被正确读取。 我不知道它是否会影响任何东西,但我确实在 Visual Studio 中编辑了文件,所以我认为这不会影响任何东西,但我提到它以防万一。 我也不止一次使用 holdLine,因为我正在阅读多个文件。

这是我正在阅读文件的地方


using (StreamReader inOFile = new StreamReader("..\\..\\options.txt"))
{               
    while (!inOFile.EndOfStream)
    {
        holdLine = inOFile.ReadLine();
        OptionsArr = holdLine.Split('\n');  
    }

有问题的文件有这些行

  • 员工ID
  • 名称
  • 部门
  • 就业状况
  • 薪水

我尝试将字符串数组的内容添加到列表框的代码

OptionsListBox.Items.Clear();

OptionsListBox.BeginUpdate();
           
//one way I tried            
string listOptions = string.Join(" ", Program.OptionsArr);
OptionsListBox.Items.Add(listOptions.ToString());
              
//different way i tried
for (int i = 0; i < Program.OptionsArr.Length; i++)
{                 
    OptionsListBox.Items.Add(Program.OptionsArr[i]);
}
                  

//another failed attempt
OptionsListBox.Items.AddRange(Program.OptionsArr);

OptionsListBox.EndUpdate();

这是一些演示问题的代码。 我添加了一些 Console.WriteLine() 来打印出循环期间和之后变量中的内容。 (这是一个小提琴

using System;
using System.IO;
                    
public class Program
{
    const string _textFile = "This is the first line in the file\n"
                           + "This is the second line in the file\n"
                           + "This is the third line in the file\n";
    public static void Main()
    {
        var utf8 = System.Text.Encoding.UTF8.GetBytes(_textFile);
        var memoryStream = new MemoryStream(utf8);
        memoryStream.Position = 0;
        
        string wholeLine;
        string[] optionsArray;

        using (var inOFile = new StreamReader(memoryStream))
        {           
            var count = 0;
            while (!inOFile.EndOfStream)
            {
                wholeLine = inOFile.ReadLine();
                optionsArray = wholeLine.Split('\n');
                
                Console.WriteLine("Reading Line {0}", ++count);
                Console.WriteLine("\tWhole: '{0}'", wholeLine);
                Console.WriteLine("\tSplit: '{0}'", string.Join("', '", optionsArray));
                Console.WriteLine()
            }
            Console.WriteLine("Final Options Array: {0}", string.Join(" | ", optionsArray));
        }
    }
}

本程序的output为:

Reading Line 1
    Whole: 'This is the first line in the file'
    Split: 'This is the first line in the file'

Reading Line 2
    Whole: 'This is the second line in the file'
    Split: 'This is the second line in the file'

Reading Line 3
    Whole: 'This is the third line in the file'
    Split: 'This is the third line in the file'

Final Options Array: This is the third line in the file

请注意 optionsArray 是如何只包含其中一项的? 它是 wholeLine 的精确副本吗? 那是因为 ReadLine() function 删除了数据中的所有换行符。 .Split('\n') 将无法拆分任何内容。

如果我将拆分字符更改为空格,则会得到:

Reading Line 1
    Whole: 'This is the first line in the file'
    Split: 'This', 'is', 'the', 'first', 'line', 'in', 'the', 'file'

Reading Line 2
    Whole: 'This is the second line in the file'
    Split: 'This', 'is', 'the', 'second', 'line', 'in', 'the', 'file'

Reading Line 3
    Whole: 'This is the third line in the file'
    Split: 'This', 'is', 'the', 'third', 'line', 'in', 'the', 'file'

Final Options Array: This | is | the | third | line | in | the | file

在这种情况下,每一行都被分成单独的单词,因为它们由空格“”分隔。 但即使我更改拆分,optionsArray 也只包含文件中的最后一行。 您的 while 循环被编写为读取整个文件,但由于您从未执行任何操作来收集拆分操作的结果,因此代码的 rest 不会执行任何操作。

尝试类似的东西:

// Employee is a class with employee fields (EmployeeID, name...)
IList<Employee> employees = readEmployeesFromFile();
OptionsListBox.DataSource = employees;
OptionsListBox.DisplayMember = "Name";
OptionsListBox.ValueMember = "EmployeeID";

暂无
暂无

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

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