列表框不会显示StreamReader文本文件中的数据

时间:2019-11-13 01:00:32

标签: c# winforms listbox

我在项目的debug文件夹中有.txt文件,但是列表框仍然不会显示任何数据。

我已经错过了这个项目的截止日期,我只有一位非常无助的教授,我很想知道我做错了什么。谢谢!

我要在此处进行的操作的概述是:

将来自三个文本文件的定界数据显示到三个列表框中。当用户单击列表框中的项目时,一行附加数据(ID)将显示在该列表框下方的文本框中。所有三个列表框以相同的形式显示,并且所有数据使用相同的字符定界。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace LabDay2Modified
{

    struct CustomersNames
    {
        public string custID;
        public string firstName;
        public string lastName;
        public string accountID;
    }

    struct AccountNumbers
    {
        public string acctID;
        public string accountType;
        public string accountNumber;
        public string accountAmt;
    }

    struct LoanInfo
    {
        public string loanID;
        public string loanYears;
        public string loanIntRate;
        public string loanAmt;
        public string customerID;
        public string loanType;
    }

    public partial class Form1 : Form
    {
        private List<CustomersNames> customerList = new List<CustomersNames>();
        private List<AccountNumbers> accountList = new List<AccountNumbers>();
        private List<LoanInfo> loanList = new List<LoanInfo>();

        public Form1()
        {
            InitializeComponent();
        }

        private void ReadCustFile()
        {
            try
            {
                StreamReader inputFile;
                string line;

                CustomersNames entry = new CustomersNames();

                char[] delim = { ',' };

                inputFile = File.OpenText("customers.txt");

                while (!inputFile.EndOfStream)
                {
                    line = inputFile.ReadLine();

                    string[] tokens = line.Split(delim);

                    entry.custID = tokens[0];
                    entry.firstName = tokens[1];
                    entry.lastName = tokens[2];
                    entry.accountID = tokens[3];

                    customerList.Add(entry);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ReadAcctFile()
        {
            try
            {
                StreamReader inputFile;
                string line;

                AccountNumbers entry = new AccountNumbers();

                char[] delim = { ',' };

                inputFile = File.OpenText("accounts.txt");

                while (!inputFile.EndOfStream)
                {
                    line = inputFile.ReadLine();

                    string[] tokens = line.Split(delim);

                    entry.acctID = tokens[0];
                    entry.accountNumber = tokens[1];
                    entry.accountType = tokens[2];
                    entry.accountAmt = tokens[3];

                    accountList.Add(entry);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ReadLoanFile()
        {
            try
            {
                StreamReader inputFile;
                string line;

                LoanInfo entry = new LoanInfo();

                char[] delim = { ',' };

                inputFile = File.OpenText("loans.txt");

                while (!inputFile.EndOfStream)
                {
                    line = inputFile.ReadLine();

                    string[] tokens = line.Split(delim);

                    entry.customerID = tokens[0];
                    entry.loanID = tokens[1];
                    entry.loanType = tokens[2];
                    entry.loanYears = tokens[3];
                    entry.loanIntRate = tokens[4];
                    entry.loanAmt = tokens[5];

                    loanList.Add(entry);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void CustInfo()
        {
            foreach(CustomersNames entry in customerList)
            {
                customerListBox.Items.Add(entry.custID + " " + entry.firstName + " " + entry.lastName);
            }
        }

        private void AcctInfo()
        {
            foreach (AccountNumbers entry in accountList)
            {
                accountListBox.Items.Add(entry.accountNumber + " " + entry.accountType + " " + entry.accountAmt);
            }
        }

        private void LoansInfo()
        {
            foreach (LoanInfo entry in loanList)
            {
                loanListBox.Items.Add(entry.loanID + " " + entry.loanType + " " + entry.loanYears+" "+entry.loanIntRate+" "+entry.loanAmt);
            }
        }



        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void customerListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = customerListBox.SelectedIndex;
            customerAccountID.Text = "Account ID: " + customerList[index].accountID;
        }

        private void loanListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = loanListBox.SelectedIndex;
            loanCustomerID.Text = "Customer ID: " + loanList[index].customerID;
        }

        private void accountListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = accountListBox.SelectedIndex;
            accountAccountID.Text = "Account ID: " + accountList[index].acctID;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ReadCustFile();
            CustInfo();

            ReadAcctFile();
            AcctInfo();

            ReadLoanFile();
            LoansInfo();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下.txt文件查看它是否适合您。

enter image description here

然后,您将得到以下结果。

enter image description here

此外,您可以为其他txt文件设置类似的样式。