Mortgage Calculator (C#)

vegaseat 0 Tallied Votes 1K Views Share

I followed Lardmeister's smart hints about creating templates with VC# 2003 and using SnippetCompiler.exe to write and test the rest of the program. It also allows you to build the executable (only 7k in size). The algorithm is from one of my Python snippets I wrote a long time ago. It only took occasional mild headscratching to convert it to C# code. The nice thing is that you end up with only 2 small files 'Mortgage.cs' and 'Mortgage.exe'. Doing the same with VC# 2005 creates over a dozen files and splits your source code up, reminds me of Delphi. I guess the apple does not fall far from the horse!

// calculate your monthly mortgage payments
// a C# Windows GUI with a Form, Labels, TextBoxes, and a Button
// compiled with the neat SnippetCompiler.exe (which uses csc.exe)
// from: http://www.sliver.com/dotnet/SnippetCompiler/
// C# and .NET Framework V2.0     vegaseat    03apr2007

using System;
using System.Windows.Forms;

namespace MortgagePayments
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      InitializeComponent();
    }

    protected override void Dispose( bool disposing )
    {
      if ( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.label3 = new System.Windows.Forms.Label();
      this.label4 = new System.Windows.Forms.Label();
      this.button1 = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.textBox2 = new System.Windows.Forms.TextBox();
      this.textBox3 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // label1
      // 
      this.label1.BackColor = System.Drawing.Color.PeachPuff;
      this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(128, 23);
      this.label1.TabIndex = 0;
      this.label1.Text = "Enter total loan ";
      // 
      // label2
      // 
      this.label2.BackColor = System.Drawing.Color.PeachPuff;
      this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label2.Location = new System.Drawing.Point(8, 40);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(128, 23);
      this.label2.TabIndex = 1;
      this.label2.Text = "Enter interest (%) ";
      // 
      // label3
      // 
      this.label3.BackColor = System.Drawing.Color.PeachPuff;
      this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label3.Location = new System.Drawing.Point(8, 72);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(128, 23);
      this.label3.TabIndex = 2;
      this.label3.Text = "Enter years to pay ";
      // 
      // label4
      // 
      this.label4.BackColor = System.Drawing.Color.MistyRose;
      this.label4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      this.label4.Font = new System.Drawing.Font("Courier New", 7.8F, 
        System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 
        ((System.Byte)(0)));
      this.label4.Location = new System.Drawing.Point(8, 152);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(272, 96);
      this.label4.TabIndex = 3;
      this.label4.Text = "Change the values to your needs";
      // 
      // button1
      // 
      this.button1.BackColor = System.Drawing.Color.Khaki;
      this.button1.Location = new System.Drawing.Point(8, 112);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(240, 23);
      this.button1.TabIndex = 4;
      this.button1.Text = "Perform Mortgage Calcuations";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(152, 8);
      this.textBox1.Name = "textBox1";
      this.textBox1.TabIndex = 5;
      this.textBox1.Text = "100000";
      //
      // textBox2
      // 
      this.textBox2.Location = new System.Drawing.Point(152, 40);
      this.textBox2.Name = "textBox2";
      this.textBox2.TabIndex = 6;
      this.textBox2.Text = "6.5";
      // 
      // textBox3
      // 
      this.textBox3.Location = new System.Drawing.Point(152, 72);
      this.textBox3.Name = "textBox3";
      this.textBox3.TabIndex = 7;
      this.textBox3.Text = "30";
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.BackColor = System.Drawing.Color.DarkGreen;
      this.ClientSize = new System.Drawing.Size(292, 260);
      this.Controls.Add(this.textBox3);
      this.Controls.Add(this.textBox2);
      this.Controls.Add(this.textBox1);
      this.Controls.Add(this.button1);
      this.Controls.Add(this.label4);
      this.Controls.Add(this.label3);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.label1);
      this.Name = "Form1";
      this.Text = "Mortgage Calculator";
      this.ResumeLayout(false);
    }
    #endregion

    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
      double principal;     // total mortgage loan
      double interestPerc;  // percent annual interest
      double interestRate;  // monthly interest rate
      double years;         // years to pay
      double paymentNum;    // number of months to pay
      double paymentVal;    // value of monthly payment
      String fstr;

      this.label4.Text = "";
      principal = double.Parse(this.textBox1.Text);
      interestPerc = double.Parse(this.textBox2.Text);
      interestRate = interestPerc/(100 * 12);
      years = double.Parse(this.textBox3.Text);
      paymentNum = years * 12;
      
      paymentVal = principal * (interestRate/(1-Math.Pow((1+interestRate), (-paymentNum))));

      fstr = String.Format("Principal Loan: {0:C}\n", principal);
      this.label4.Text += fstr;
      this.label4.Text += "Interest (%)  : " + interestPerc + '\n';
      this.label4.Text += "Years to pay  : " + years + '\n';
      this.label4.Text += "Months to pay : " + paymentNum + '\n';
      fstr = String.Format("Monthly pay   : {0:C}\n", paymentVal);
      this.label4.Text += fstr;
      fstr = String.Format("Total pay     : {0:C}\n", paymentVal*paymentNum);
      this.label4.Text += fstr;
    }
  }
}
Ene Uran 638 Posting Virtuoso

Nice to know that C# has gained popularity apart from the original MS Visual IDE. It can be used on Linux platforms using Mono instead of .NET. There is also an official language standard at:
http://www.ecma-international.org/publications/standards/Ecma-334.htm

I think the GUI capabilities are great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.