📄 formmain.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SuperCalculator
{
public partial class FormMain : Form
{
private double startAmount;
private double yearRate;
private int years;
private string calculateFrequency;
object sender = new object();
EventArgs e = new EventArgs();
public FormMain()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
}
private void FormMain_Load(object sender, EventArgs e)
{
textBoxstartAmount.Focus();
}
private void FormMain_Shown(object sender, EventArgs e)
{
textBoxstartAmount.Focus();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
//保证修改任意输入值时,不显示计算结果
textBoxTotal.Clear();
}
/// <summary>
/// 计算事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCalculate_Click(object sender, EventArgs e)
{
CaculateResult();
}
/// <summary>
/// 计算事件实现函数
/// </summary>
private void CaculateResult()
{
string Amount = textBoxstartAmount.Text.Trim();
try
{
startAmount = Double.Parse(Amount);
}
catch (Exception)
{
MessageBox.Show("含有非法字符,请重新输入!");
return;
}
if (startAmount < 100.0)
MessageBox.Show("存款金额不得低于100");
string Rate = textBoxyearRate.Text.Trim();
try
{
yearRate = Double.Parse(Rate) / 100;
}
catch (Exception)
{
MessageBox.Show("含有非法字符,请重新输入!");
return;
}
if (yearRate >= 1.0)
MessageBox.Show("利率输入错误!");
try
{
years = Int32.Parse(textBoxyears.Text.Trim());
}
catch (Exception)
{
MessageBox.Show("含有非法字符,请重新输入!");
return;
}
if (comboBoxcalculateFrequency.SelectedIndex == -1)
{
MessageBox.Show("请选择提供的利息计算方式");
return;
}
calculateFrequency = comboBoxcalculateFrequency.SelectedItem.ToString();
switch (calculateFrequency)
{
case "按月计息":
textBoxTotal.Text = Caculate(startAmount, yearRate / 12, years*12).ToString("f2");
break;
case "按季度计息":
textBoxTotal.Text = Caculate(startAmount, yearRate / 4, years*4).ToString("f2");
break;
case "按年计息":
textBoxTotal.Text = Caculate(startAmount, yearRate, years).ToString("f2");
break;
}
}
/// <summary>
/// 计算本息总额函数
/// </summary>
/// <param name="startAmount"></param>
/// <param name="rate"></param>
/// <param name="count"></param>
/// <returns></returns>
private double Caculate(double startAmount, double rate, int count)
{
return startAmount * Math.Pow((1 + rate), count);
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox txt=(TextBox)sender;
if (e.KeyCode == Keys.Enter)
{
switch(txt.Name)
{
case "textBoxstartAmount":
textBoxyearRate.Focus();
break;
case "textBoxyearRate":
textBoxyears.Focus();
break;
case "textBoxyears":
comboBoxcalculateFrequency.Focus();
comboBoxcalculateFrequency.SelectedIndex = 0;
break;
}
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
textBoxTotal.Text = "";
}
private void comboBoxcalculateFrequency_SelectedIndexChanged(object sender, EventArgs e)
{
textBoxTotal.Text = "";
}
private void buttonCalculate_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
CaculateResult();
}
private void comboBoxcalculateFrequency_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
buttonCalculate.Focus();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -