一、設計思路
用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網友們拋磚引玉,其功能上還有很多不完善的地方,歡迎網友們將其完善。
新聞熱點
疑難解答