0

我在 Visual Studio 2012 中创建了一个控制台应用程序,它根据本金、贷款和利息计算贷款。我现在正在尝试将此控制台应用程序转换为 Windows 窗体。我觉得这个过程应该只是关掉控制台提示并用 Windows Forms txt 和单选按钮替换它。
这是我的控制台应用程序代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MortgageCalculator
{
    public class MortgageCalculator
    {

        public static void Main(string[] args)
        {
            // declare variables
            double principle = 0; 
            double years = 0;
            double interest = 0;
            string principleInput, yearsInput, interestInput;


            // User input for Principle amount in dollars
            Console.Write("Enter the loan amount, in dollars(0000.00): ");
            principleInput = Console.ReadLine();
            principle = double.Parse(principleInput);
            //Prompt the user to reenter any illegal input
            if (principle < 0)
            {
                Console.WriteLine("The value for the mortgage cannot be a negative value");
            principle = 0;
        }


        // User input for number of years
        Console.Write("Enter the number of years: ");
        yearsInput = Console.ReadLine();
        years = double.Parse(yearsInput);
        //Prompt the user to reenter any illegal input
        if (years < 0)
        {
            Console.WriteLine("Years cannot be a negative value");
            years = 0;
        }

        // User input for interest rate
        Console.Write("Enter the interest rate(%): ");
        interestInput = Console.ReadLine();
        interest = double.Parse(interestInput);
        //Prompt the user to reenter any illegal input
        if (interest < 0)
        {
            Console.WriteLine("The value for the interest rate cannot be a negative value");
            interest = 0;
        }

        //Calculate the monthly payment
        //ADD IN THE .Net function call Math.pow(x, y) to compute xy (x raised to the y power). 
        double loanM = (interest / 1200.0);
        double numberMonths = years * 12;
        double negNumberMonths = 0 - numberMonths;
        double monthlyPayment = principle * loanM / (1 - System.Math.Pow((1 + loanM),   negNumberMonths));

        //double totalPayment = monthlyPayment;


        //Output the result of the monthly payment
        Console.WriteLine(String.Format("The amount of the monthly payment is: {0}{1:0.00}", "$", monthlyPayment));
        Console.WriteLine();
        Console.WriteLine("Press the Enter key to end. . .");
        Console.Read();

        }
    }
}

我现在想将以上内容转换为 Windows 窗体应用程序。我想要的设计是我的表单,原则上是一个文本框,然后我有三个单选按钮控件。一个用于 15 年、30 年和其他(这是另一个文本框)。利率是一个组合框,利率为 1-15%。这是我第一次尝试创建 Windows 窗体,我将在下面展示我目前所拥有的内容。我想我的主要问题是计算我的每月付款以及如何将其导入 Windows 窗体应用程序。任何对正确方向的帮助将不胜感激。

   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;

   namespace LoanCalc
   {
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           tbLoanDuration.Enabled = false;
       }

       private void rb30years_CheckedChanged(object sender, EventArgs e)
       {
           if (rbOther.Checked)
               tbLoanDuration.Enabled = true;
           else
               tbLoanDuration.Enabled = false;
       }

       private void button1_Click(object sender, EventArgs e)
       {

          if (cbLoanRate.SelectedIndex > -1)
           {
               string s = cbLoanRate.SelectedItem.ToString();
           }

           string msg = string.Format("Your total monthly payment is", MonthlyPayment);



        }

       private void tbLoanDuration_TextChanged(object sender, EventArgs e)
       {
           double years = 0;
           Control ctrl = (Control)sender;

           bool success = Double.TryParse(tbLoanDuration.Text, out years);
           if (!success)
           {
               errorProvider1.SetError(ctrl, "This is not a valid number");
           }  
           else
           {
               errorProvider1.SetError(ctrl, "");
           }

       }

       private double MonthlyPayment()
       {
           double loanM = (interest / 1200.0);
           double numberMonths = years * 12;
           double negNumberMonths = 0 - numberMonths;
           double monthlyPayment = principle * loanM / (1 - System.Math.Pow((1 + loanM), negNumberMonths));

           return monthlyPayment;
       }




       public double interest { get; set; }
       public double years { get; set; }
       public double principle { get; set; }

       private void tbPrinciple_TextChanged(object sender, EventArgs e)
       {

           double principle = Double.Parse(tbPrinciple.Text);
           //Prompt the user to reenter any illegal input
           if (principle < 0)
           {
               Console.WriteLine("The value for the mortgage cannot be a negative value");
               principle = 0;
           }
       } 
    }

}
4

1 回答 1

0

正如 PhaDaPhunk 所说,让我们确切地知道您遇到的问题(错误消息、不确定如何获取数据等)对我们帮助您非常有帮助。根据您问题的一般性,我看到了一些事情,其中​​一些我在我的评论中提出,我将在这里详细说明。

您在代码中定义了三个属性:

   public double interest { get; set; }
   public double years { get; set; }
   public double principle { get; set; }

在风格上,属性通常是大写的(即public double Interest)。

当相应控件的值或选定项发生更改时,您似乎正在为这些属性分配值 - 至少有时是这样。我将对您的代码进行以下更改:

private void rb30years_CheckedChanged(object sender, EventArgs e)
{

    if (rb15Years.Checked)
    {
        years = 15;
        tbLoanDuration.Enabled = false;
    }
    else if (rb30Years.Checked)
    {
        years = 30;
        tbLoanDuration.Enabled = false;
    }       
    else if (rbOther.Checked)
    {
        tbLoanDuration.Enabled = true;
    }
}

我猜你的 15 年和 30 年单选按钮 ID 是什么。year如果他们选择 15 或 30,这也会为属性分配一个值。

对于您的计算,您需要调用该方法并将返回结果格式化为字符串:

private void button1_Click(object sender, EventArgs e)
{

    double selectedRate = 0;

    if (cbLoanRate.SelectedIndex > 1 && double.TryParse(cbLoanRate.SelectedItem, out selectedRate))
    {
        interest = selectedRate;

        lblMonthlyPayment.Text = string.Format("Your total monthly payment is {0}{1:0.00}", "$", MonthlyPayment());
    }
}

这是大部分更改的地方。

首先,如果 cbLoanRate 的 selectedIndex 大于 1,并且选中的项可以转换为 double,则进行计算,否则什么也不做(或显示错误)。

如果 selectedIndex 大于 1 并且选中的 item 可以解析为 doulbe,则将结果赋给selectedRate属性interest

我不确定你打算做什么msg,但假设你有一个Label电话lblMonthlyPayment,显示每月付款。我使用了与控制台应用程序中相同的字符串格式,并直接调用了您的方法MonthlyPayment()。看起来您试图monthlyPayment从该MonthlyPayment方法中使用,但这不起作用,因为它仅在方法内部的范围内(可访问)。然后将方法的返回值分配给lblMonthlyPayment的 Text 属性。

最后,我也会改变你tbPrinciple_TextChanged的一点:

   private void tbPrinciple_TextChanged(object sender, EventArgs e)
   {

       // Use TryParse instead of Parse in case the user enters text or some
       // other non-numeric character
       double enteredPrinciple = 0;

       if (double.TryParse(tbPrincipal.Text, out enteredPrinciple))
       {
           if (enteredPrinciple < 0)
           {
               // Console.WriteLine won't display anything to the user
               MessageBox.Show("The value for the mortgage cannot be a negative value");
           }
           else
           {
               // Assign it to the property
               principle = enteredPrinciple;
           }
       }
       else
       {
           // Prompt the user to enter a numeric value
           MessageBox.Show("The value for the mortgage must be a numeric value");
       }
   } 

tbLoanDuration_TextChanged最后,对您的方法稍作改动:

   private void tbLoanDuration_TextChanged(object sender, EventArgs e)
   {
       // Use a different name for the out parameter than your property
       // to avoid confusion
       double enteredYears = 0;

       // This is unnecessary
       Control ctrl = (Control)sender;

       if (Double.TryParse(tbLoanDuration.Text, out enteredYears))
       {
           years = enteredYears;
           errorProvider1.SetError(ctrl, "");
       }  
       else
       {
           years = 0;
           errorProvider1.SetError(ctrl, "This is not a valid number");
       }
   }

这些更改中的大多数都是次要的 - 总而言之,您走在正确的道路上。我还没有测试过这段代码,大约 2 年没有做过 WinForms,所以可能存在一些语法问题,但这应该会为您指明正确的方向。

于 2013-04-24T16:50:43.223 回答