Click here to Skip to main content
15,895,841 members
Articles / Desktop Programming / WPF

Loan Amortization Application in WPF using VC++

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
10 Jul 2009CPOL22 min read 61.5K   2.1K   27  
This article discusses how to do WPF programming in VC++ and one working example of Loan Amortization with some background.
#include "MainWindow.h"
#include "PaymentWindow.h"

MainWindow::MainWindow()
{
	Title = L"Amortization";
	Height = 300;
	Width = 400;
	WindowStyle = Windows::WindowStyle::ToolWindow;
	ResizeMode = Windows::ResizeMode::NoResize;
	WindowStartupLocation = Windows::WindowStartupLocation::CenterScreen;
	
	Grid^ grid = gcnew Grid();

	grid->Background::set(Media::Brushes::Khaki);

	// Create Rows in grid layout
	Windows::Controls::RowDefinition^ row1 = gcnew Windows::Controls::RowDefinition();
	Windows::Controls::RowDefinition^ row2 = gcnew Windows::Controls::RowDefinition();
	Windows::Controls::RowDefinition^ row3 = gcnew Windows::Controls::RowDefinition();
	Windows::Controls::RowDefinition^ row4 = gcnew Windows::Controls::RowDefinition();
	Windows::Controls::RowDefinition^ row5 = gcnew Windows::Controls::RowDefinition();

	// Create Columns in grid layout
	Windows::Controls::ColumnDefinition^ col1 = gcnew Windows::Controls::ColumnDefinition();
	Windows::Controls::ColumnDefinition^ col2 = gcnew Windows::Controls::ColumnDefinition();

	// Add all the rows into grid
	grid->RowDefinitions->Add(row1);
	grid->RowDefinitions->Add(row2);
	grid->RowDefinitions->Add(row3);
	grid->RowDefinitions->Add(row4);
	grid->RowDefinitions->Add(row5);

	// Add all the columns into grid
	grid->ColumnDefinitions->Add(col1);
	grid->ColumnDefinitions->Add(col2);

	// Principle Label
	lblPrinciple = gcnew Label();
	lblPrinciple->Margin = Thickness(5);
	lblPrinciple->Content = L"Principle Amount";
	lblPrinciple->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(lblPrinciple, 0);
	grid->SetColumn(lblPrinciple, 0);

	// Principle text
	txtPrinciple = gcnew TextBox();
	txtPrinciple->Margin = Thickness(5);
	txtPrinciple->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(txtPrinciple, 0);
	grid->SetColumn(txtPrinciple, 1);

	// Interest Label
	lblInterest = gcnew Label();
	lblInterest->Margin = Thickness(5);
	lblInterest->Content = L"Interest Rate";
	lblInterest->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(lblInterest, 1);
	grid->SetColumn(lblInterest, 0);

	// Interest Rate text
	txtInterest = gcnew TextBox();
	txtInterest->Margin = Thickness(5);
	txtInterest->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(txtInterest, 1);
	grid->SetColumn(txtInterest, 1);

	// No of Years Label
	lblYears = gcnew Label();
	lblYears->Margin = Thickness(10);
	lblYears->Content = L"No of Years";
	lblYears->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(lblYears, 2);
	grid->SetColumn(lblYears, 0);

	// No of Years text
	txtYears = gcnew TextBox();
	txtYears->Margin = Thickness(5);
	txtYears->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(txtYears, 2);
	grid->SetColumn(txtYears, 1);

	// Payment Method Label
	lblPaymentMethod = gcnew Label();
	lblPaymentMethod->Margin = Thickness(5);
	lblPaymentMethod->Content = L"Payment Method";
	lblPaymentMethod->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(lblPaymentMethod, 3);
	grid->SetColumn(lblPaymentMethod, 0);

	// Payment Method Combo Box
	cmbPaymentMethod = gcnew ComboBox();
	cmbPaymentMethod->Margin = Thickness(5, 15, 5, 15);
	cmbPaymentMethod->SelectionChanged += gcnew SelectionChangedEventHandler(this, &MainWindow::OnSelectionChange);
	grid->SetRow(cmbPaymentMethod, 3);
	grid->SetColumn(cmbPaymentMethod, 1);

	// Payment Button
	btnPayment = gcnew Button();
	btnPayment->Margin = Thickness(25, 10, 25, 10);
	btnPayment->Content = L"Payment";
	btnPayment->Click += gcnew RoutedEventHandler(this, &MainWindow::OnBtnPayment);
	grid->SetRow(btnPayment, 4);
	grid->SetColumn(btnPayment, 0);

	// Exit Button
	btnExit = gcnew Button();
	btnExit->Margin = Thickness(25, 10, 25, 10);
	btnExit->Content = L"Exit";
	btnExit->Click += gcnew RoutedEventHandler(this, &MainWindow::OnBtnExit);
	grid->SetRow(btnExit, 4);
	grid->SetColumn(btnExit, 1);

	// Add childern into grid layout
	grid->Children->Add(lblPrinciple);
	grid->Children->Add(txtPrinciple);
	grid->Children->Add(lblInterest);
	grid->Children->Add(txtInterest);
	grid->Children->Add(lblYears);
	grid->Children->Add(txtYears);
	grid->Children->Add(lblPaymentMethod);
	grid->Children->Add(cmbPaymentMethod);
	grid->Children->Add(btnPayment);
	grid->Children->Add(btnExit);
	Content = grid;

	// Fill the combo box with default values
	cmbPaymentMethod->Items->Add(L"Monthly");
	cmbPaymentMethod->Items->Add(L"Yearly");
	cmbPaymentMethod->Items->Add(L"Quartely");
	cmbPaymentMethod->Items->Add(L"Bi-Yearly");
	cmbPaymentMethod->Items->Add(L"Daily");
	cmbPaymentMethod->SelectedIndex = 0;
}

void MainWindow::OnBtnExit(Object^ sender, RoutedEventArgs^ e)
{
	Close();
}

void MainWindow::OnSelectionChange(Object^ sender, SelectionChangedEventArgs^ e)
{
	switch (cmbPaymentMethod->SelectedIndex)
	{
	case 0:
		paymentMethod = Monthly;
		break;

	case 1:
		paymentMethod = Yearly;
		break;

	case 2:
		paymentMethod = Quartly;
		break;

	case 3:
		paymentMethod = BiYearly;
		break;

	case 4:
		paymentMethod = Daily;
		break;
	}
}

void MainWindow::OnBtnPayment(Object^ sender, RoutedEventArgs^ e)
{
	try
	{
		// get all the information from the user interface
		// and pass it to the PaymentWindow class
		PaymentWindow^ payment = gcnew PaymentWindow();
		
		double interestRate = Double::Parse(txtInterest->Text);
		double amount = Double::Parse(txtPrinciple->Text);
		int years = Int32::Parse(txtYears->Text);

		payment->Method = paymentMethod;
		payment->InterestRate = interestRate;
		payment->Principle = amount;
		payment->Years = years;		

		payment->Owner = this;
		payment->Show();
	}
	catch(Exception^ e)
	{
		MessageBox::Show(e->Message, L"Error", MessageBoxButton::OK, MessageBoxImage::Error);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader American Institute for Research
United States United States
Working as a Team leader in American Institute for Research

Comments and Discussions