[visual-studio]尝试转换到 Windows 窗体 VS 2012 年一个控制台应用程序

发布时间: 2014/3/1 3:44:58
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

计算基于原则、 贷款和利息贷款的 Visual Studio 2012 年,我创建了一个控制台应用程序。我现在想转换该控制台应用程序的 Windows 窗体。我觉得这个过程应该是只是交换机的控制台提示和替换 Windows 窗体 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 年来,和其他 (这是另一个 textbox)。利率是从 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;
           }
       } 
    }

}

解决方法 1:

正如 PhaDaPhunk 所说,让我们知道您所遇到的到底什么问题 (错误消息,不知道如何获取数据,等等) 对我们帮助你在任何时候都很有帮助。我就根据你的问题的一般性的原则下,看到几件事,的几个,其中我放在我的评论中,我会在这里详细阐述。

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

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

在文体方面,属性通常是 capitilized (即, 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,并且选定的项目可以转换为一个双精度型,然后做计算,否则为什么也不做 (或显示错误)。

如果在选定的项目可被分析为显影 selectedIndex 大于 1,将分配的结果 selectedRateinterest 属性。

不知道你打算如何处理 msg ,但我说的你有 Label 称为 lblMonthlyPayment ,显示的每月付款。我使用相同的字符串格式你在控制台应用程序中,并调用您的方法 MonthlyPayment() 直接。它看起来像你想要使用 monthlyPaymentMonthlyPayment 方法,和那不工作就是仅在方法内 (访问) 的范围内。然后您的方法的返回值分配给 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 年,所以可能有句法的几个问题,但这应指向你在正确的方向前进。

赞助商