在c#教学的第二个案例前,先介绍了一个储蓄计算器的实验,该实验项目是从微软的教学光盘中取出的,部分源程序已经给出,要求学生完成事务处理部分。在做这个实验的时候,要求学生最好能够独立设计此项目。在这里,将该项目的代码给出如下。有特点的是,控件的名称使用的是中文。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace saving
{
enum compound
{
每月计算利息,
每季度计算利息
}
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.label label1;
private system.windows.forms.numericupdown 初始金额;
private system.windows.forms.label label2;
private system.windows.forms.numericupdown 利率;
private system.windows.forms.label label3;
private system.windows.forms.label label4;
private system.windows.forms.combobox 计算规则;
private system.windows.forms.label label5;
private system.windows.forms.label label6;
private system.windows.forms.button 计算;
private system.windows.forms.numericupdown 存期;
private system.windows.forms.numericupdown 每月存入;
private system.windows.forms.numericupdown 存款总额;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
计算规则.items.add(compound.每月计算利息);
计算规则.items.add(compound.每季度计算利息);
计算规则.selecteditem = 计算规则.items[0];
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.label1 = new system.windows.forms.label();
this.初始金额 = new system.windows.forms.numericupdown();
this.label2 = new system.windows.forms.label();
this.利率 = new system.windows.forms.numericupdown();
this.label3 = new system.windows.forms.label();
this.存期 = new system.windows.forms.numericupdown();
this.label4 = new system.windows.forms.label();
this.计算规则 = new system.windows.forms.combobox();
this.label5 = new system.windows.forms.label();
this.每月存入 = new system.windows.forms.numericupdown();
this.label6 = new system.windows.forms.label();
this.存款总额 = new system.windows.forms.numericupdown();
this.计算 = new system.windows.forms.button();
((system.componentmodel.isupportinitialize)(this.初始金额)).begininit();
((system.componentmodel.isupportinitialize)(this.利率)).begininit();
((system.componentmodel.isupportinitialize)(this.存期)).begininit();
((system.componentmodel.isupportinitialize)(this.每月存入)).begininit();
((system.componentmodel.isupportinitialize)(this.存款总额)).begininit();
this.suspendlayout();
//
// label1
//
this.label1.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label1.location = new system.drawing.point(32, 32);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(88, 16);
this.label1.tabindex = 0;
this.label1.text = "初始金额:";
//
// 初始金额
//
this.初始金额.decimalplaces = 2;
this.初始金额.increment = new system.decimal(new int[] {
100,
0,
0,
0});
this.初始金额.location = new system.drawing.point(112, 32);
this.初始金额.maximum = new system.decimal(new int[] {
-1156317184,
46566128,
0,
0});
this.初始金额.name = "初始金额";
this.初始金额.size = new system.drawing.size(168, 21);
this.初始金额.tabindex = 1;
this.初始金额.thousandsseparator = true;
this.初始金额.value = new system.decimal(new int[] {
1000,
0,
0,
0});
//
// label2
//
this.label2.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label2.location = new system.drawing.point(32, 72);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(88, 16);
this.label2.tabindex = 0;
this.label2.text = "利率(%):";
//
// 利率
//
this.利率.decimalplaces = 2;
this.利率.increment = new system.decimal(new int[] {
1,
0,
0,
65536});
this.利率.location = new system.drawing.point(112, 72);
this.利率.name = "利率";
this.利率.size = new system.drawing.size(168, 21);
this.利率.tabindex = 1;
this.利率.thousandsseparator = true;
this.利率.value = new system.decimal(new int[] {
20,
0,
0,
65536});
//
// label3
//
this.label3.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label3.location = new system.drawing.point(32, 112);
this.label3.name = "label3";
this.label3.size = new system.drawing.size(88, 16);
this.label3.tabindex = 0;
this.label3.text = "存期(年):";
//
// 存期
//
this.存期.location = new system.drawing.point(112, 112);
this.存期.name = "存期";
this.存期.size = new system.drawing.size(168, 21);
this.存期.tabindex = 1;
this.存期.thousandsseparator = true;
this.存期.value = new system.decimal(new int[] {
5,
0,
0,
0});
//
// label4
//
this.label4.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label4.location = new system.drawing.point(32, 152);
this.label4.name = "label4";
this.label4.size = new system.drawing.size(88, 16);
this.label4.tabindex = 0;
this.label4.text = "计算规则:";
//
// 计算规则
//
this.计算规则.location = new system.drawing.point(112, 152);
this.计算规则.maxdropdownitems = 2;
this.计算规则.name = "计算规则";
this.计算规则.size = new system.drawing.size(168, 20);
this.计算规则.tabindex = 2;
//
// label5
//
this.label5.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label5.location = new system.drawing.point(32, 192);
this.label5.name = "label5";
this.label5.size = new system.drawing.size(88, 16);
this.label5.tabindex = 0;
this.label5.text = "每月存入:";
//
// 每月存入
//
this.每月存入.decimalplaces = 2;
this.每月存入.location = new system.drawing.point(112, 192);
this.每月存入.maximum = new system.decimal(new int[] {
-1156317184,
46566128,
0,
0});
this.每月存入.name = "每月存入";
this.每月存入.size = new system.drawing.size(168, 21);
this.每月存入.tabindex = 1;
this.每月存入.thousandsseparator = true;
//
// label6
//
this.label6.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label6.location = new system.drawing.point(32, 264);
this.label6.name = "label6";
this.label6.size = new system.drawing.size(88, 16);
this.label6.tabindex = 0;
this.label6.text = "存款总额:";
//
// 存款总额
//
this.存款总额.decimalplaces = 2;
this.存款总额.increment = new system.decimal(new int[] {
100,
0,
0,
0});
this.存款总额.location = new system.drawing.point(112, 264);
this.存款总额.maximum = new system.decimal(new int[] {
-1156317184,
46566128,
0,
0});
this.存款总额.name = "存款总额";
this.存款总额.readonly = true;
this.存款总额.size = new system.drawing.size(168, 21);
this.存款总额.tabindex = 1;
this.存款总额.thousandsseparator = true;
//
// 计算
//
this.计算.location = new system.drawing.point(248, 320);
this.计算.name = "计算";
this.计算.tabindex = 3;
this.计算.text = "计算存款";
this.计算.click += new system.eventhandler(this.计算_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(360, 373);
this.controls.add(this.计算);
this.controls.add(this.存款总额);
this.controls.add(this.每月存入);
this.controls.add(this.计算规则);
this.controls.add(this.存期);
this.controls.add(this.利率);
this.controls.add(this.初始金额);
this.controls.add(this.label1);
this.controls.add(this.label2);
this.controls.add(this.label3);
this.controls.add(this.label4);
this.controls.add(this.label5);
this.controls.add(this.label6);
this.name = "form1";
this.text = "储蓄利率计算器";
((system.componentmodel.isupportinitialize)(this.初始金额)).endinit();
((system.componentmodel.isupportinitialize)(this.利率)).endinit();
((system.componentmodel.isupportinitialize)(this.存期)).endinit();
((system.componentmodel.isupportinitialize)(this.每月存入)).endinit();
((system.componentmodel.isupportinitialize)(this.存款总额)).endinit();
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void 计算_click(object sender, system.eventargs e)
{
double startamount=(double)初始金额.value;
double rate=(double)利率.value;
int years=(int)存期.value;
compound calcfrequency=(compound) 计算规则.selecteditem;
double additional=(double)每月存入.value;
decimal totalvalue=0;
rate=rate/100;
int months=12*years;
switch (calcfrequency)
{
case compound.每月计算利息:
double monthlyrate=rate/12;
for (int i=1; i<=months; i++)
{
startamount+=startamount*monthlyrate;
startamount+=additional;
}
break;
case compound.每季度计算利息:
double quarterlyrate=rate/4;
for (int i=1; i<=months; i++)
{
if (i%3==0)
{
startamount+=startamount*quarterlyrate;
}
startamount+=additional;
}
break;
default:
messagebox.show("该项工作尚未实现!");
break;
}
totalvalue=(decimal)startamount;
存款总额.value=totalvalue;
}
}
}