【问题标题】:Doing calculation using array data selected in a combobox使用组合框中选择的数组数据进行计算
【发布时间】:2017-04-28 22:28:59
【问题描述】:

我正在尝试编写一个贷款程序,以便当用户从组合框中选择一个选项时,它会在列表框中显示该选择的数据(贷款金额、当前贷款金额等)。然后,如果用户点击一个按钮,它会从当前贷款金额中减去一笔付款。我遇到的问题是让它处理组合框中选择的贷款的数据,并在列表框中减去和显示新的贷款金额。这是我写的代码:

        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

数据格式为:贷款号、原余额、本期余额、利率、期限、月供

【问题讨论】:

    标签: c# arrays winforms combobox


    【解决方案1】:

    你有:

    string selection = comboBox1.SelectedIndex.ToString();
    

    与:

    if (loans[row, 0] == selection)
    

    SelectedIndex() 属性将返回选择的 Index,从 0(零)到 Count() - 1;如果当前未进行任何选择,则返回 -1...因此您的“选择”变量将从不匹配贷款值(ID?)。

    您需要改用SelectedItem()

    string selection = comboBox1.SelectedItem.ToString();
    

    【讨论】:

    • 太棒了!这有助于解决这个问题。现在的问题是我在 currentLoan = int.Parse(loans[row, 2]);
    【解决方案2】:

    由于您开始编写代码,首先您应该为 Loan 创建一个类,其中包含您的不同属性(贷款编号、金额、利息等)。

    然后将成员添加到您的表单中,该成员是贷款对象列表

    public List<Loan> MyLoans {get; set;}
    

    在 form_load 从您的文本中读取构建此列表并将其设置为 MyLoans 属性。

    创建一个接收列表并将其绑定到组合框的函数。

    在组合框中,贷款编号为 datavalue 字段,而贷款金额为 displayvalue 字段

    然后在按钮单击事件上,不要打扰选定的索引或其他任何东西。只需获取应让您访问贷款编号的所选项目。

    对 MyLoans 进行 linq 查询并更改您想要在 Loan 上更改的任何属性

    MyLoans.Where(t=>t.LoanNumber == selecteditem).Single().LoanAmount -= payment; //or something like that
    

    现在您的贷款已更新,因此您可以再次调用将组合框与 MyLoans 绑定的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2021-02-23
      • 1970-01-01
      相关资源
      最近更新 更多