二、开发过程
下面跟我们一步步学习吧.
先启动vs.net,建一个空的c#项目,如下(pic1.jpg)
名称用wincalc,位置就自己设定好了。确定后,从vs.net开发环境中右边的工具箱中加入控件表的控件,,注意修改txtcalc的如属性readonly=true,righttoleft=yes(从右至左输入),最后画面如何(pic2)
添加代码如源程序所示:
最后大功告成,按ctrl+f5吧,看看结果.如图(pic3.jpg)
三、源程序
//基本的计算器
//蚕蛹 2001.11.26
//using c#
//e-mail:sillnet@hotmail.com
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
namespace wincalc
{
///
/// summary description for calcform.
///
public class calcform : system.windows.forms.form
{
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.button button3;
private system.windows.forms.button button4;
private system.windows.forms.button button5;
private system.windows.forms.button button6;
private system.windows.forms.button button7;
private system.windows.forms.button button8;
private system.windows.forms.button button9;
private system.windows.forms.button button10;
private system.windows.forms.button bclr;
private system.windows.forms.button bdot;
private system.windows.forms.button bplus;
private system.windows.forms.button bsub;
private system.windows.forms.button bmul;
private system.windows.forms.button bdiv;
private system.windows.forms.button bequ;
private system.windows.forms.textbox txtcalc;
//以下是要添加的代码
//定义变量
double dblacc;
double dblsec;
bool blnclear,blnfrstopen;
string stroper;
//以上是添加的代码
///
/// required designer variable.
///
private system.componentmodel.container components = null;
public calcform()
{
//
// required for windows form designer support
//
initializecomponent();
//
// todo: add any constructor code after initializecomponent call
//
//以下是要添加的代码
//初始化设量
dblacc=0;
dblsec=0;
blnfrstopen=true;
blnclear=true;
stroper=new string(=,1);
//以上是添加的代码
}
///
/// clean up any resources being used.
///
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
///
/// required method for designer support – do not modify
/// the contents of this method with the code editor.
///
private void initializecomponent()
{
this.bplus = new system.windows.forms.button();
this.bmul = new system.windows.forms.button();
this.bdot = new system.windows.forms.button();
this.txtcalc = new system.windows.forms.textbox();
this.bclr = new system.windows.forms.button();
this.bdiv = new system.windows.forms.button();
this.bsub = new system.windows.forms.button();
this.button8 = new system.windows.forms.button();
this.button9 = new system.windows.forms.button();
this.bequ = new system.windows.forms.button();
this.button10 = new system.windows.forms.button();
this.button4 = new system.windows.forms.button();
this.button5 = new system.windows.forms.button();
this.button6 = new system.windows.forms.button();
this.button7 = new system.windows.forms.button();
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.button3 = new system.windows.forms.button();
this.suspendlayout();
//
// bplus
//
this.bplus.backcolor = system.drawing.systemcolors.control;
this.bplus.forecolor = system.drawing.systemcolors.controltext;
this.bplus.location = new system.drawing.point(208, 112);
this.bplus.name = “bplus”;
this.bplus.size = new system.drawing.size(32, 80);
this.bplus.tabindex = 1;
this.bplus.text = “+”;
//以下是要添加的代码
bplus.click += new system.eventhandler(this.btn_oper);
//以上是添加的代码
//
// bmul
//
this.bmul.location = new system.drawing.point(160, 112);
this.bmul.name = “bmul”;
this.bmul.size = new system.drawing.size(32, 32);
this.bmul.tabindex = 1;
this.bmul.text = “*”;
//以下是要添加的代码
bmul.click += new system.eventhandler(this.btn_oper);
//以上是添加的代码
//
// bdot
//
this.bdot.forecolor = system.drawing.color.black;
this.bdot.location = new system.drawing.point(112, 208);
this.bdot.name = “bdot”;
this.bdot.size = new system.drawing.size(32, 32);
this.bdot.tabindex = 0;
this.bdot.text = “.”;
//以下是要添加的代码
bdot.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// txtcalc
//
this.txtcalc.location = new system.drawing.point(16, 24);
this.txtcalc.name = “txtcalc”;
this.txtcalc.readonly = true;
this.txtcalc.righttoleft = system.windows.forms.righttoleft.yes;
this.txtcalc.size = new system.drawing.size(224, 21);
this.txtcalc.tabindex = 2;
this.txtcalc.text = “”;
//
// bclr
//
this.bclr.backcolor = system.drawing.systemcolors.control;
this.bclr.forecolor = system.drawing.systemcolors.controltext;
this.bclr.location = new system.drawing.point(208, 64);
this.bclr.name = “bclr”;
this.bclr.size = new system.drawing.size(32, 32);
this.bclr.tabindex = 0;
this.bclr.text = “ac”;
//以下是要添加的代码
bclr.click += new system.eventhandler(this.btn_clr);
//以上是添加的代码
//
// bdiv
//
this.bdiv.location = new system.drawing.point(160, 160);
this.bdiv.name = “bdiv”;
this.bdiv.size = new system.drawing.size(32, 32);
this.bdiv.tabindex = 1;
this.bdiv.text = “/”;
//以下是要添加的代码
bdiv.click += new system.eventhandler(this.btn_oper);
//以上是添加的代码
//
// bsub
//
this.bsub.location = new system.drawing.point(160, 64);
this.bsub.name = “bsub”;
this.bsub.size = new system.drawing.size(32, 32);
this.bsub.tabindex = 1;
this.bsub.text = “-“;
//以下是要添加的代码
bsub.click += new system.eventhandler(this.btn_oper);
//以上是添加的代码
//
// button8
//
this.button8.location = new system.drawing.point(16, 64);
this.button8.name = “button8”;
this.button8.size = new system.drawing.size(32, 32);
this.button8.tabindex = 0;
this.button8.text = “7”;
//以下是要添加的代码
button8.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// button9
//
this.button9.location = new system.drawing.point(64, 64);
this.button9.name = “button9”;
this.button9.size = new system.drawing.size(32, 32);
this.button9.tabindex = 0;
this.button9.text = “8”;
//以下是要添加的代码
button9.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// bequ
//
this.bequ.backcolor = system.drawing.systemcolors.control;
this.bequ.forecolor = system.drawing.systemcolors.controltext;
this.bequ.location = new system.drawing.point(160, 208);
this.bequ.name = “bequ”;
this.bequ.size = new system.drawing.size(80, 32);
this.bequ.tabindex = 1;
this.bequ.text = “=”;
//以下是要添加的代码
bequ.click += new system.eventhandler(this.btn_equ);
//以上是添加的代码
//
// button10
//
this.button10.location = new system.drawing.point(112, 64);
this.button10.name = “button10”;
this.button10.size = new system.drawing.size(32, 32);
this.button10.tabindex = 0;
this.button10.text = “9”;
//以下是要添加的代码
button10.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// button4
//
this.button4.location = new system.drawing.point(112, 160);
this.button4.name = “button4”;
this.button4.size = new system.drawing.size(32, 32);
this.button4.tabindex = 0;
this.button4.text = “3”;
//以下是要添加的代码
button4.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// button5
//
this.button5.location = new system.drawing.point(16, 112);
this.button5.name = “button5”;
this.button5.size = new system.drawing.size(32, 32);
this.button5.tabindex = 0;
this.button5.text = “4”;
//以下是要添加的代码
button5.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// button6
//
this.button6.location = new system.drawing.point(64, 112);
this.button6.name = “button6”;
this.button6.size = new system.drawing.size(32, 32);
this.button6.tabindex = 0;
this.button6.text = “5”;
//以下是要添加的代码
button6.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// button7
//
this.button7.location = new system.drawing.point(112, 112);
this.button7.name = “button7”;
this.button7.size = new system.drawing.size(32, 32);
this.button7.tabindex = 0;
this.button7.text = “6”;
//以下是要添加的代码
button7.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// button1
//
this.button1.backcolor = system.drawing.systemcolors.control;
this.button1.forecolor = system.drawing.color.black;
this.button1.location = new system.drawing.point(16, 208);
this.button1.name = “button1”;
this.button1.size = new system.drawing.size(80, 32);
this.button1.tabindex = 0;
this.button1.text = “0”;
//以下是要添加的代码
button1.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// button2
//
this.button2.location = new system.drawing.point(16, 160);
this.button2.name = “button2”;
this.button2.size = new system.drawing.size(32, 32);
this.button2.tabindex = 0;
this.button2.text = “1”;
//以下是要添加的代码
button2.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// button3
//
this.button3.location = new system.drawing.point(64, 160);
this.button3.name = “button3”;
this.button3.size = new system.drawing.size(32, 32);
this.button3.tabindex = 0;
this.button3.text = “2”;
//以下是要添加的代码
button3.click += new system.eventhandler(this.btn_clk);
//以上是添加的代码
//
// calcform
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(256, 261);
this.controls.addrange(new system.windows.forms.control[] {
this.txtcalc,
this.bequ,
this.bdiv,
this.bmul,
this.bsub,
this.bplus,
this.bdot,
this.bclr,
this.button10,
this.button9,
this.button8,
this.button7,
this.button6,
this.button5,
this.button4,
this.button3,
this.button2,
this.button1});
this.name = “calcform”;
this.text = “计算器”;
this.resumelayout(false);
}
#endregion
//以下是要添加的代码
//小数点的操作
private void btn_clk(object obj,eventargs ea){
if(blnclear)
txtcalc.text=””;
button b3=(button)obj;
txtcalc.text+=b3.text;
if(txtcalc.text==”.”)
txtcalc.text=”0.”;
dblsec=convert.todouble(txtcalc.text);
blnclear=false;
}
//程序开始点
private static void main(){
application.run(new calcform());
}
private void btn_oper(object obj,eventargs ea){
button tmp=(button)obj;
stroper=tmp.text;
if(blnfrstopen)
dblacc=dblsec;
else
calc();
blnfrstopen=false;
blnclear=true;
}
//等号运算
private void btn_equ(object obj,eventargs ea){
calc();
}
//所有运算操作
private void calc(){
switch(stroper){
case “+”:
dblacc+=dblsec; //加号运算
break;
case “-“:
dblacc-=dblsec; //减号运算
break;
case “*”:
dblacc*=dblsec; //乘号运算
break;
case “/”:
dblacc/=dblsec; //除号运算
break;
}
stroper=”=”; //等号运算
blnfrstopen=true;
txtcalc.text=convert.tostring(dblacc);//将运算结果转换成字符类型,并输出
dblsec=dblacc;//将运算数a的值放入运算数b中,以便后面运算
}
//清除按钮
private void btn_clr(object obj,eventargs ea){
clear();
}
//清除按钮的操作
private void clear(){
dblacc=0;
dblsec=0;
blnfrstopen=true;
txtcalc.text=””;
txtcalc.focus();//设置焦点为txtcalc
}
//以上是添加的代码
}
}
以上只是一个简单的用c#开发的windows form程序,在vs.nt bate2+windows 2000专业版编译通过.向正在学习vs.net网友们抛砖引玉,其功能上还有很多不完善的地方,欢迎网友们将其完善。