簡體   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