一、设计思路 
   
    用c#写的比较基础的windows form 程序,该计算器实现了基础的数学运算,如加,减,乘,除等任务.主要是通过该程序学习vs.net的 
   
    编程环境,以及windows form程序.主要针对初学者 
   
    我们分两部份来实现程序, 
   
    第一部份.程序界面 
   
    1,以下控件表 
   
  控件类型 name text 
  form calcform 计算器 
  button button1 0 
   ..... 
   button10 9 
   bdot .(小数点) 小数点按钮 
   bplus +(加号) 加号按钮 
   bsub -(减号) 减号按钮 
   bmul *(乘号) 乘号按钮 
   bdiv /(除号) 除号按钮 
   bequ =(等号) 等号按钮 
   bclr ac 清除按钮 
   textbox txtcalc (空值) 用来显示输入及输出结果 
   
    第二部份,程序结构 
   
    1,定义以下变量 
   
  double dblacc; //运算数a 
  double dblsec; //运算数b 
  bool blnclear,blnfrstopen;//布尔类型用来判断清除与否,以及第一个显示字符 
  string stroper;//通过获取stroper的值来决定运算+,-,*,/,= 
   
    2,用以下方法来实现按钮的动作 
   
    例: bdot.click+=net eventhandler(btn_clk);//eventhandler类是事件代表类,用来注册事件的处理方法. 
   
    //第一个参数是object类型,指向发出事件的对象; 
   
    //第二个参数是eventargs类型,包含了关于这个事件的数据 
   
    3,用以下方法来判断运算以及运算操作 
   
  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; 
  } 
   
    4,小数点运算 
   
  //先判断是否已经按了小数点按钮,如果按了,最0.x来代替运算变量,并且将转换成double数型数值 
  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; 
  } 
   
    程序中所涉及到的一些问题,都给解决了,现在我们动手吧!操上我的利器,去完成任务吧!
  源程序 
   
  //基本的计算器 
  //蚕蛹 2001.11.26 
  //using c# 
  //e-mail:[email protected] 
  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网友们抛砖引玉,其功能上还有很多不完善的地方,欢迎网友们将其完善。
新闻热点
疑难解答