我在 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;
}
}
}
}