我在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年和其他(这是另一个文本框)。利率为ComboBox,利率为1-15%。这是我第一次尝试创建Windows窗体,我将在下面展示我的内容。我想我的主要问题是如何计算我的monthlyPayment以及如何将其导入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;
}
}
}
}
答案 0 :(得分: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是什么。如果选择15或30,这也会为year
属性赋值。
对于您的计算,您需要调用方法并将返回结果格式化为字符串:
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且所选项目可以解析为doulbe,请将selectedRate
的结果分配给interest
属性。
我不确定您打算使用msg
做什么,但是假设您有一个名为Label
的{{1}}来显示每月付款。我使用了与控制台应用程序中相同的字符串格式,并直接调用了方法lblMonthlyPayment
。看起来您试图使用MonthlyPayment()
方法中的monthlyPayment
,但这不起作用,因为它只在方法内的范围内(可访问)。然后将方法的返回值分配给MonthlyPayment
的Text属性。
最后,我还会改变你的lblMonthlyPayment
:
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
这些变化中的大多数都是次要的 - 总而言之,你正走在正确的道路上。我没有测试过这段代码,并且在大约2年内没有完成WinForms,所以可能会有一些语法问题,但这应该指向正确的方向。