我试图编写贷款计划,以便当用户从组合框中选择一个选项时,它会在列表框中显示该选择的数据(贷款金额,当前贷款金额等)。然后,如果用户单击按钮,则从当前贷款金额中减去付款。我遇到的问题是它正在处理组合框中选择的贷款的数据,并在列表框中减去并显示新的贷款金额。这是我写的代码:
public Form1()
{
InitializeComponent();
}
string[,] loans = new string[4, 6];
int recordCount = 0;
private void Form1_Load(object sender, EventArgs e)
{
string currentLine;
string[] fields = new string[6];
int row = 0;
StreamReader loanReader = new StreamReader(@"C:\loans.txt");
while (loanReader.EndOfStream == false)
{
currentLine = loanReader.ReadLine();
fields = currentLine.Split(',');
loans[row, 0] = fields[0];
loans[row, 1] = fields[1];
loans[row, 2] = fields[2];
loans[row, 3] = fields[3];
loans[row, 4] = fields[4];
loans[row, 5] = fields[5];
row = row + 1;
}
recordCount = row;
loanReader.Close();
int nbrRows = 4;
txtPrincipal.Text = "0";
for (int i = 0; i < nbrRows; i++)
{
comboBox1.Items.Add(loans[i, 0]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int row = comboBox1.SelectedIndex;
lstDisplay.Items.Clear();
string fmtStr = "{0,-15}{1,8}{2,30}";
string fmtStr2 = "{0,-15}{1,8:C}{2,40:C}";
lstDisplay.Items.Add(string.Format(fmtStr, "Loan #", "Original Balance", "Current Balance"));
lstDisplay.Items.Add(string.Format(fmtStr2, loans[row, 0], loans[row, 1], loans[row,2]));
}
private void btnRP_Click(object sender, EventArgs e)
{
int row = 0, currentLoan, interest, interestPmt, monthlyPmt, principalPmt, newBalance;
string selection = comboBox1.SelectedIndex.ToString();
for (row = 0; row < recordCount; row++)
{
if (loans[row, 0] == selection)
{
currentLoan = int.Parse(loans[row, 2]);
interest = int.Parse(loans[row, 3]);
monthlyPmt = int.Parse(loans[row, 5]);
interestPmt = currentLoan * interest / 1200;
principalPmt = monthlyPmt - interestPmt;
newBalance = currentLoan - principalPmt;
currentLoan = newBalance;
lstDisplay.Items.Clear();
string fmtStr = "{0,-15}{1,8}{2,30}";
string fmtStr2 = "{0,-15}{1,8:C}{2,40:C}";
lstDisplay.Items.Add(string.Format(fmtStr, "Loan #", "Original Balance", "Current Balance"));
lstDisplay.Items.Add(string.Format(fmtStr2, loans[row, 0], loans[row, 1], currentLoan));
}
}
}
代码在用户点击按钮更新当前贷款金额之前首先正确显示组合框信息,但是一旦点击按钮,它就不会改变列表框中的贷款金额。我怎样才能解决这个问题?
如果代码混乱,我很抱歉,我刚刚开始学习如何编写代码。此外,如果有帮助,这里是数组中的数据:
1001,55000,46326.26,7,30,352.61
1010,30000,11757.26,5,15,228.61
1003,1000,406.35,5,1,82.49
1005,5000,2042.72,3,2,207.09
数据格式为贷款编号,原始余额,当前余额,利率,期限,每月付款
答案 0 :(得分:0)
由于您正在开始编码,首先您应该使用不同的属性(贷款数量,金额,利息,等等)为贷款上课。
然后将一个成员添加到您的表单中,该成员是一个贷款对象列表
public List<Loan> MyLoans {get; set;}
在form_load上,从构建此列表的文本中读取并将其设置为MyLoans属性。
创建一个接收列表并将其绑定到组合框的函数。
在组合框中,贷款编号为datavalue字段,贷款金额为displayvalue字段
然后在按钮点击事件上,不要打扰所选索引或其他任何内容。只需获取所选项目即可获得贷款编号。
对MyLoans执行linq查询并更改贷款
上您想要的任何属性MyLoans.Where(t=>t.LoanNumber == selecteditem).Single().LoanAmount -= payment; //or something like that
现在您的贷款已更新,因此您可以再次调用该功能,将组合框与MyLoans绑定。
答案 1 :(得分:0)
你有:
Rc
使用:
string selection = comboBox1.SelectedIndex.ToString();
SelectedIndex()属性将返回选择的索引,它将为0(零)到Count() - 1;如果当前没有选择,则返回-1 ...所以你的“选择”变量从不匹配贷款价值(ID?)。
您需要使用if (loans[row, 0] == selection)
代替:
SelectedItem()