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 is i'm having trouble with coding the math formula properly with the textbox, radio buttons, and combobox. Im not sure how to include the Radio Button options(15,30,other)that is selected when running the program into the formula properly.
This is the code I have: I know that im not incorporating the radiobox options into the formula bellow. Currently when I run the program i'm getting the validation error that i've setup: "Please enter a valid length" and "Please enter a valid interest rate"
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
{
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 (!float.TryParse(InterestRateCB.SelectedItem.ToString(), out r))
MessageBox.Show("Please Enter a Valid Interest Rate", "Incorrect 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;
}
}
}