简体   繁体   English

C#从多个文本文件读取,将行拆分为一个列表,然后加载到ListBox中

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

I'm getting a few errors and also my code is unfinished. 我遇到一些错误,我的代码也未完成。 I was using another Stackoverflow question to set this up to begin with but it wasn't fit to my needs. 我在使用另一个Stackoverflow问题来进行设置,但它不符合我的需求。

I have three text files which the data is split by commas such as "Name,25,25.6" so string, int, decimal. 我有三个文本文件,数据用逗号分隔,例如“ Name,25,25.6”,即字符串,整数,十进制。 I have all three text files that have three columns like that, same data types, but just different names/numbers. 我所有的三个文本文件都具有类似的三列,相同的数据类型,但名称/数字不同。

I have three different list boxes that I want to split them into but I'm having trouble getting the three different split list items to get into three different list boxes. 我想将它们拆分成三个不同的列表框,但是在将三个不同的拆分列表项放入三个不同的列表框中时遇到了麻烦。 I'll copy and paste all the code I have. 我将复制并粘贴所有我拥有的代码。 I am also using a combo box to allow the user to select the file they want to load into the combo box which I believe I got it right. 我还使用了一个组合框,以允许用户选择要加载到组合框中的文件,我相信我做对了。

The errors I get are in the displayLists(), it says on the lstItemName.DataSource = Inventory; 我得到的错误在displayLists()中,它在lstItemName.DataSource = Inventory上显示; line that Inventory does not exist in the current context. 表示库存在当前上下文中不存在。 There are also a plenitude of other errors. 还有许多其他错误。

Any help will be appreciated, I'll copy and paste my code. 任何帮助将不胜感激,我将复制并粘贴我的代码。 I have a Windows Form and I'm using Visual Studio Express 2012 in C# 我有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

I do not know if this is exactly what you need, but try something like this: 我不知道这是否正是您需要的,但是尝试这样的操作:

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