I've created a Mortgage Calculator via WPF on visual studio 2012. The design looks like this:
Enter Loan amount: TextBox here
Enter Loan Duration:
15 years(Radio Button)
30 years(Radio Button)
Other(Radio Button) with TextBox
Select Interest Rate: ComboBox here
Button here to calculate
The issue i'm having trouble with coding the math formula properly with the textbox, radio buttons, and combobox all in the formula. I'm having problems getting the selected item in the combobox in the equation correctly and the radio buttons as well. As of now when I run the program and caluculate it It says "The Amount of the monthly payment is: NaN"
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplicationCALC
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonCal_Click(object sender, RoutedEventArgs e)
{
double p;
float r;
double n;
if (!double.TryParse(Textboxloan.Text, out p))
MessageBox.Show("Please Enter a Valid Amount", "Incorrect Data");
if (double.TryParse(Textboxother.Text, out n))
MessageBox.Show("Please Enter a Valid Length. \n Amount given is: " + Textboxother.Text);
if (Radio15.IsChecked== true)
{
n = 15;
}
if (Radio30.IsChecked == true)
{
n = 30;
}
if (float.TryParse(InterestRateCB.SelectedItem.ToString(), out r))
MessageBox.Show("Enter a Valid Interest Rate");
double top = p * r / 1200.00;
double bottom = 1 - Math.Pow(1.0 + r / 1200.0, -12.0 * n);
double monthly = top / bottom;
string MonthPay = string.Format("The amount of the monthly payment is: {0:c}", monthly);
MessageBox.Show(MonthPay);
}
private void Radio_Checked(object sender, RoutedEventArgs e)
{
if (RadioOther.IsChecked == true)
{
Textboxother.IsEnabled = true;
}
else
{
Textboxother.IsEnabled = false;
}
}
private void InterestRateCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem cpi = InterestRateCB.SelectedItem as ComboBoxItem;
string str = (string)cpi.Content;
}
}
}