【问题标题】:C# Reading from multiple text files, splitting lines into a List, and then loading into ListBoxC#从多个文本文件中读取,将行拆分为一个List,然后加载到ListBox
【发布时间】:2017-05-11 00:25:00
【问题描述】:

我遇到了一些错误,而且我的代码还没有完成。我正在使用另一个 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

【问题讨论】:

  • 您是否创建了表单列表?

标签: c# list listview combobox


【解决方案1】:

我不知道这是否正是您所需要的,但请尝试以下方法:

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; }
}

【讨论】:

  • 我实际上是在大约 10 分钟前自己想出来的,但我非常感谢您的回复,我做到了 {get; set} 并将 DataSources 重置为 null ,这很有效!再次感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多