【发布时间】:2019-11-13 01:00:32
【问题描述】:
我在项目的调试文件夹中有 .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();
}
}
}
【问题讨论】:
-
Tagging help page: “你应该在标题中使用标签的唯一时间是它们与标题的对话语气有机。”
[visual-studio]tag info page: “不要在有关代码的问题上使用这个标签,这些代码恰好是用 Visual Studio 编写的” -
entry = new CustomersNames();这需要在 while 循环中完成。对于所有三个文件加载函数 -
我已经测试了您提供的代码,但我可以在列表框中显示数据。我猜它可能与您的txt文件有关。可以提供txt文件吗?
-
@JackJJun-MSFT 以下是文本文件:customers.txt 1,Ann,Porter,5 2,William,Porter,5 3,Daniel,Higgins,2 4,Mary,Langan,2 5 ,Luis,Delgado,3 6,Kelly,Stratman,4 7,Gerry,Yates,1 8,Greta,Mickel,1 9,James,Podell,6 10,Emily,Lowe,6 accounts.txt 1,1234,1,950.25 2 ,2345,2,100.5 3,3456,2,3452.23 4,4567,1,1500 5,5678,1,602.75 6,6789,2,5000 loan.txt 1,1,1,30,3.75,200000 2,2,2, 3,0.90,10000 4,3,2, 5,1.90,30000 3,4,1,30,4.00,300000 6,5,1,15,3.50,100000 7,6,2, 5,1.90,15000 5 ,7,2, 5,1.90,18000 9,8,1,30,4.20,270000