簡體   English   中英

C#從多個文本文件讀取,將行拆分為一個列表,然后加載到ListBox中

[英]C# Reading from multiple text files, splitting lines into a List, and then loading into ListBox

我遇到一些錯誤,我的代碼也未完成。 我在使用另一個Stackoverflow問題來進行設置,但它不符合我的需求。

我有三個文本文件,數據用逗號分隔,例如“ Name,25,25.6”,即字符串,整數,十進制。 我所有的三個文本文件都具有類似的三列,相同的數據類型,但名稱/數字不同。

我想將它們拆分成三個不同的列表框,但是在將三個不同的拆分列表項放入三個不同的列表框中時遇到了麻煩。 我將復制並粘貼所有我擁有的代碼。 我還使用了一個組合框,以允許用戶選擇要加載到組合框中的文件,我相信我做對了。

我得到的錯誤在displayLists()中,它在lstItemName.DataSource = Inventory上顯示; 表示庫存在當前上下文中不存在。 還有許多其他錯誤。

任何幫助將不勝感激,我將復制並粘貼我的代碼。 我有Windows窗體,並且在C#中使用Visual Studio Express 2012

namespace TCSCapstone
{
public partial class frmInventory : Form
{
    public frmInventory()
    {
        InitializeComponent();
    }

    string cstrItemName;
    int cintNumberOfItems;
    decimal cdecPrice;
    decimal cdecTotalPrices;

    string selectedList = "";

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        selectedList = this.cmbList.GetItemText(this.cmbList.SelectedItem);

        if (selectedList == "Creative Construction")//if the selected combo 
box item equals the exact string selected
        {
            selectedList = "creative"; //then the string equals creative, 
which is creative.txt but I add the .txt in the btnLoadInfo method
        } else if (selectedList == "Paradise Building")
        {
            selectedList = "paradise";//this is for paradise.txt
        }
        else if (selectedList == "Sitler Construction")
        {
            selectedList = "sitler";//this is for sitler.txt
        }
        else
        {
            MessageBox.Show("Please select one of the items.");
        }
    }

    private void btnLoadInfo_Click(object sender, EventArgs e)
    {
        List<frmInventory> Inventory = new List<frmInventory>();
        using (StreamReader invReader = new StreamReader(selectedList + 
".txt"))
        {
            while (invReader.Peek() >= 0)
            {
                string str;
                string[] strArray;
                str = invReader.ReadLine();

                strArray = str.Split(',');
                frmInventory currentItem = new frmInventory();
                currentItem.cstrItemName = strArray[0];
                currentItem.cintNumberOfItems = int.Parse(strArray[1]);
                currentItem.cdecPrice = decimal.Parse(strArray[2]);

                Inventory.Add(currentItem);

            }
        }
        displayLists();
    }//end of btnLoadInfo

    void displayLists()
    {
        int i;
        lstItemName.Items.Clear();
        lstNumberOfItems.Items.Clear();
        lstPrice.Items.Clear();
        lstTotalPrices.Items.Clear();

        lstItemName.DataSource = Inventory;
        lstItemName.ValueMember = "cstrItemName";
        lstItemName.DisplayMember = "cintNumberOfItems";
    }

}//end of frmInventory
}//end of namespace

我不知道這是否正是您需要的,但是嘗試這樣的操作:

public partial class Form2 : Form
{

    List<Inventory> inventory;
    public Form2()
    {
        InitializeComponent();
    }

    public void ReadFiles()
    {
        if (inventory == null)
            inventory = new List<Inventory>();

        using (TextReader r = new StreamReader("file.txt"))
        {
            string line = null;
            while ((line = r.ReadLine()) != null)
            {
                string[] fields = line.Split(',');
                Inventory obj = new Inventory();
                obj.Name = fields[0];
                obj.Qtd = Convert.ToInt32(fields[1]);
                obj.Price = Convert.ToInt32(fields[2]);

                inventory.Add(obj);
            }
        }

        SetDataSourceList();


    }

    public void SetDataSourceList()
    {
        listBox1.DisplayMember = "Name";
        listBox2.DisplayMember = "Qtd";
        listBox3.DisplayMember = "Price";
        listBox1.DataSource =
            listBox2.DataSource = 
            listBox3.DataSource =
            inventory;
    }



}

public class Inventory
{
    public string Name { get; set; }
    public int Qtd { get; set; }
    public decimal Price { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM