C#Windows窗体应用程序 - 初学者并且不确定如何继续下一步

时间:2014-03-06 02:09:49

标签: c# .net winforms visual-studio-2010

我是C#的新手,我正在尝试自我教育,以便在工作中使用它。

我正在尝试编写财务规划程序。我的目标是让它接受用户计划保存的金额(财务目标),年度投资和利率。每次按下“明年”按钮时,程序将计算年末的余额,其中包括先前的余额加上按输入的利率递增的年度投资。它还将显示当前余额与输入的财务目标之间的差额。我希望使用货币格式来显示余额和剩余金额。

我在下面提供了一个视觉示例,说明了我如何进行检索,但数字是手动输入的,因为我很难使代码工作。

enter image description here

以下是我的代码的当前状态:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Assignment_1___1._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        int year = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            year ++;
            double goal = 0;

            if (double.TryParse(txtGoal.Text, out goal))
            {
                txtYear.Text = year.ToString();
            }

            else
            {
                MessageBox.Show("Please enter a value!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

尽管我尽最大努力在网上搜索,但我还是不知道如何继续下去。我认为这是由于我目前缺乏与C#编程相关的知识。剩下的就是让'Balance'和'Remaining'文本框按照设想填写。

6 个答案:

答案 0 :(得分:1)

我建议只将值存储为小数并将其显示/解析为货币

ToString for currency

var displayValue = currencyDec.ToString('c');

Parse Currency

if (!Decimal.TryParse(inputValue, out currencyDec))
        throw new ArgumentException('inputValue');

答案 1 :(得分:0)

您可以使用+,这是一个连接运算符

txtBalance.Text="$"+ dblBalance.toString();

这里txtBalance是一个文本框,dblBalalnce是余额。

答案 2 :(得分:0)

看看......

http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

你想要

textBox.Text = string.Format(CultureInfo.CurrentUICulture, "{0:C2}", value);

... OR

textBox.Text = value.ToString("C2", CultureInfo.CurrentUICulture);

答案 3 :(得分:0)

这对你有用......

public partial class Form1 : Form
{
    private Int32 _CurrentYear = 0;
    private Double _CurrentBalance = 0;
    private Double _CurrentRemaining = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBoxGoal.Text = "100000";
        textBoxYearlyInvest.Text = "2000";
        textBoxInterestRate.Text = "0.06";
        textBoxYear.Text = "1";
        textBoxBalance.Text = "0";
        textBoxRemaining.Text = textBoxGoal.Text;
    }

    private void buttonNextYear_Click(object sender, EventArgs e)
    {
        // Increment the current year.
        _CurrentYear++;

        // Calculate the current balance and remaining diff from goal.
        _CurrentBalance += (Double)Int32.Parse(textBoxYearlyInvest.Text);
        _CurrentBalance += _CurrentBalance * Double.Parse(textBoxInterestRate.Text);
        _CurrentRemaining = (Double)(Int32.Parse(textBoxGoal.Text) - _CurrentBalance);

        // Populate the form elements.
        textBoxYear.Text = _CurrentYear.ToString();
        textBoxBalance.Text = Math.Round(_CurrentBalance, 2).ToString("C");
        textBoxRemaining.Text = Math.Round(_CurrentRemaining, 2).ToString("C");
    }

    private void buttonClose_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

答案 4 :(得分:0)

您可以将数据绑定用于此类目的,因为它有助于从代码中填充数据并从用户读回数据,以及显示格式化而不会有太多麻烦:

public partial class Form1 : Form
{
    public int Year {get; set;}
    public decimal Balance {get; set;}
    public decimal Remaining {get; set;}
    public decimal YearlyGoal {get; set;}
    public decimal YearlyInvest {get; set;}
    public decimal InterestRate {get; set;}

    public Form1()
    {
        InitializeComponent();
        this.Year = 0;
        this.Balance = 0;
        this.Remaining = 0;
        this.YearlyGoal = 100000;
        this.YearlyInvest = 2000;
        this.InterestRate = 0.06;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.textBoxYear.DataBindings.Add("Text", this, "Year");
        this.textBoxBalance.DataBindings.Add("Text", this, "Balance", true, DataSourceUpdateMode.OnValidation, null, "c");
        this.textBoxRemaining.DataBindings.Add("Text", this, "Remaining", true, DataSourceUpdateMode.OnValidation, null, "c");
        this.textBoxYearlyGoal.DataBindings.Add("Text", this, "YearlyGoal", true, DataSourceUpdateMode.OnValidation, null, "c");
        this.textBoxYearlyInvest.DataBindings.Add("Text", this, "YearlyInvest", true, DataSourceUpdateMode.OnValidation, null, "c");
        this.textBoxInterestRate.DataBindings.Add("Text", this, "InterestRate");
    }

    private void buttonNextYear_Click(object sender, EventArgs e)
    {
        this.CurrentYear++;

        this.CurrentBalance += this.YearlyInvest;
        this.CurrentBalance += this,CurrentBalance * this.InterestRate;
        this.CurrentRemaining = this.YearlyGoal - this.CurrentBalance;
    }

    private void buttonClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

答案 5 :(得分:0)

在代码中手动初始化文本框,然后只需将框中字符串的转换值减去浮点数,即可显示目标中的剩余数量。

相关问题