尝试在VS 2012中将控制台应用程序转换为Windows窗体
本文关键字:程序转换 应用 Windows 窗体 控制台 VS 2012 | 更新日期: 2023-09-27 18:10:48
我在Visual Studio 2012中创建了一个控制台应用程序,它根据原则,贷款和利息计算贷款。我现在正尝试将此控制台应用程序转换为Windows窗体。我觉得这个过程应该只是把控制台提示换成Windows窗体文本和单选按钮。
以下是我的控制台应用程序代码:
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;
}
}
}
}
正如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,且所选项可以解析为双值,则将selectedRate
的结果赋值给interest
属性。
我不确定你打算用msg
做什么,但假设你有一个名为lblMonthlyPayment
的Label
,以显示每月付款。我使用了您在控制台应用程序中使用的相同字符串格式,并直接调用您的方法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");
}
}
大多数的改变都是小的——总之你走在正确的道路上。我没有测试过这段代码,也没有做过WinForms大约2年了,所以可能会有一些语法问题,但这应该指向正确的方向。