C# Windows Forms Application - Beginner and unsure how to proceed next
C# Windows Forms Application - Beginner and unsure how to proceed next
I am new to C# and am trying to self educate myself in order to use it at work.
I am trying to write a program for financial planning. My goal is to have it accept the amount the user is planning to save (financial goal), yearly investment, and an interest rate. Each time ?Next year? button is pressed the program would calculate the balance at the end of the year, which includes previous balance plus the yearly investment incremented by the entered interest rate. It would also show by how much the current balance is short of the entered financial goal. Preferably, I would like to use currency format to show the balance and the remaining amount.
I have provided a visual example below of how I evision it, however the numbers were manually entered as I am having difficulty making the code to work.
The following is the current state of my code that works great:
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(); } } }
I am at a loss of how to proceed next, despite my best efforts to search online. I assume this is due to my current lack of knowledge related to C# programming. All that is remaining is to make the 'Balance' and 'Remaining' text boxes fill out as envisioned.
Answer by Spiritz for C# Windows Forms Application - Beginner and unsure how to proceed next
You can use + which is a concatenation operator
txtBalance.Text="$"+ dblBalance.toString();
Here txtBalance is a textbox and dblBalalnce is the balance amount.
Answer by Gent for C# Windows Forms Application - Beginner and unsure how to proceed next
I would recommend simply storing the value as a decimal and displaying/parsing it as a currency
var displayValue = currencyDec.ToString('c');
if (!Decimal.TryParse(inputValue, out currencyDec)) throw new ArgumentException('inputValue');
Answer by Mick for C# Windows Forms Application - Beginner and unsure how to proceed next
Have a look at...
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
You want
textBox.Text = string.Format(CultureInfo.CurrentUICulture, "{0:C2}", value);
OR...
textBox.Text = value.ToString("C2", CultureInfo.CurrentUICulture);
Answer by husterk for C# Windows Forms Application - Beginner and unsure how to proceed next
This should work for you...
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(); } }
Answer by Alejandro for C# Windows Forms Application - Beginner and unsure how to proceed next
You may use data binding for such purposes, as it helps with populating data from your code and reading it back from the user, as well as display formating without too much of a hassle:
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(); } }
Answer by Ronnie for C# Windows Forms Application - Beginner and unsure how to proceed next
Initialize your textboxes manually in code, then show your remaining from the amount in the goal simply by subtracting the converted value of the string in the box to a float.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment