初学C#时写的比较基础的windows Form 程序,该计算器实现了基础的数学运算,如加,减,乘,除等任务.主要是通过该程序学习vs.net的编程环境,以及windows Form程序. 如图
步骤如下:
1.新建一个windows应用程序
2.在窗体上添加21个按钮作为计算器的按键,并设计这些控件的Name和Text属性,再在表单中添加一个文本框控件,将其Text属性设为空。
3.设置清除按钮btn_clear,并添加清除所有全局变量及文本框内容的代码,在form1.cs中定义一个方法append_num(i),用于处理编辑框中输入的数字;为数字为0的按钮添加click事件btuuon0_click,在该事件的处理代码中处理所有数字的输入,然后其他的数字按钮共享该代码,所有这些按钮通过sender对象来确定当前用户选择的安钮;为”+“号按钮添加click事件btn_plus_click事件,并使log,exp,sqrt等按钮的ckick事件共享该事件的代码。
下面是Form1.cs的代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace jisuanqi
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : 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 btn_clear;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
public double dblFirst=0.0;//用来存放第一个操作数
public double dblSecond=0.0;//用来存放第二个操作数
public bool blnFirstOper=true; //判断是不是第一个操作符
public bool blnClear=false;
public String strOper; //记录操作符
private System.Windows.Forms.Button btn_dot;
private System.Windows.Forms.Button btn_equ;
private System.Windows.Forms.Button btn_plus;
private System.Windows.Forms.Button btn_minus;
private System.Windows.Forms.Button btn_mul;
private System.Windows.Forms.Button btn_div;
private System.Windows.Forms.Button btn_abs;
private System.Windows.Forms.Button btn_sqrt;
private System.Windows.Forms.Button btn_exp;
private System.Windows.Forms.Button btn_log;
private System.Windows.Forms.Button button0;
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = 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.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button0 = new System.Windows.Forms.Button();
this.btn_dot = new System.Windows.Forms.Button();
this.btn_equ = new System.Windows.Forms.Button();
this.btn_clear = new System.Windows.Forms.Button();
this.btn_plus = new System.Windows.Forms.Button();
this.btn_minus = new System.Windows.Forms.Button();
this.btn_mul = new System.Windows.Forms.Button();
this.btn_div = new System.Windows.Forms.Button();
this.btn_abs = new System.Windows.Forms.Button();
this.btn_sqrt = new System.Windows.Forms.Button();
this.btn_exp = new System.Windows.Forms.Button();
this.btn_log = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.Location = new System.Drawing.Point(24, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 32);
this.button1.TabIndex = 0;
this.button1.Text = "1";
this.button1.Click += new System.EventHandler(this.button0_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button2.Location = new System.Drawing.Point(104, 184);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 32);
this.button2.TabIndex = 1;
this.button2.Text = "2";
this.button2.Click += new System.EventHandler(this.button0_Click);
//
// button3
//
this.button3.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button3.Location = new System.Drawing.Point(184, 184);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(48, 32);
this.button3.TabIndex = 2;
this.button3.Text = "3";
this.button3.Click += new System.EventHandler(this.button0_Click);
//
// button4
//
this.button4.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button4.Location = new System.Drawing.Point(24, 128);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(48, 32);
this.button4.TabIndex = 3;
this.button4.Text = "4";
this.button4.Click += new System.EventHandler(this.button0_Click);
//
// button5
//
this.button5.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button5.Location = new System.Drawing.Point(104, 128);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(48, 32);
this.button5.TabIndex = 4;
this.button5.Text = "5";
this.button5.Click += new System.EventHandler(this.button0_Click);
//
// button6
//
this.button6.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button6.Location = new System.Drawing.Point(184, 128);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(48, 32);
this.button6.TabIndex = 5;
this.button6.Text = "6";
this.button6.Click += new System.EventHandler(this.button0_Click);
//
// button7
//
this.button7.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button7.Location = new System.Drawing.Point(24, 80);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(48, 32);
this.button7.TabIndex = 6;
this.button7.Text = "7";
this.button7.Click += new System.EventHandler(this.button0_Click);
//
// button8
//
this.button8.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button8.Location = new System.Drawing.Point(104, 80);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(48, 32);
this.button8.TabIndex = 7;
this.button8.Text = "8";
this.button8.Click += new System.EventHandler(this.button0_Click);
//
// button9
//
this.button9.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button9.Location = new System.Drawing.Point(184, 80);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(48, 32);
this.button9.TabIndex = 8;
this.button9.Text = "9";
this.button9.Click += new System.EventHandler(this.button0_Click);
//
// button0
//
this.button0.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button0.Location = new System.Drawing.Point(24, 240);
this.button0.Name = "button0";
this.button0.Size = new System.Drawing.Size(48, 32);
this.button0.TabIndex = 9;
this.button0.Text = "0";
this.button0.Click += new System.EventHandler(this.button0_Click);
//
// btn_dot
//
this.btn_dot.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_dot.Location = new System.Drawing.Point(104, 240);
this.btn_dot.Name = "btn_dot";
this.btn_dot.Size = new System.Drawing.Size(48, 32);
this.btn_dot.TabIndex = 10;
this.btn_dot.Text = ".";
this.btn_dot.Click += new System.EventHandler(this.btn_dot_Click);
//
// btn_equ
//
this.btn_equ.Font = new System.Drawing.Font("LiSu", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_equ.Location = new System.Drawing.Point(184, 240);
this.btn_equ.Name = "btn_equ";
this.btn_equ.Size = new System.Drawing.Size(48, 32);
this.btn_equ.TabIndex = 11;
this.btn_equ.Text = "=";
this.btn_equ.Click += new System.EventHandler(this.btn_equ_Click);
//
// btn_clear
//
this.btn_clear.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_clear.Location = new System.Drawing.Point(24, 16);
this.btn_clear.Name = "btn_clear";
this.btn_clear.Size = new System.Drawing.Size(48, 32);
this.btn_clear.TabIndex = 12;
this.btn_clear.Text = "C";
this.btn_clear.Click += new System.EventHandler(this.btn_clear_Click);
//
// btn_plus
//
this.btn_plus.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_plus.Location = new System.Drawing.Point(264, 240);
this.btn_plus.Name = "btn_plus";
this.btn_plus.Size = new System.Drawing.Size(48, 32);
this.btn_plus.TabIndex = 13;
this.btn_plus.Text = "+";
this.btn_plus.Click += new System.EventHandler(this.btn_plus_Click);
//
// btn_minus
//
this.btn_minus.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_minus.Location = new System.Drawing.Point(264, 184);
this.btn_minus.Name = "btn_minus";
this.btn_minus.Size = new System.Drawing.Size(48, 32);
this.btn_minus.TabIndex = 14;
this.btn_minus.Text = "-";
this.btn_minus.Click += new System.EventHandler(this.btn_plus_Click);
//
// btn_mul
//
this.btn_mul.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_mul.Location = new System.Drawing.Point(264, 128);
this.btn_mul.Name = "btn_mul";
this.btn_mul.Size = new System.Drawing.Size(48, 32);
this.btn_mul.TabIndex = 15;
this.btn_mul.Text = "*";
this.btn_mul.Click += new System.EventHandler(this.btn_plus_Click);
//
// btn_div
//
this.btn_div.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_div.Location = new System.Drawing.Point(264, 80);
this.btn_div.Name = "btn_div";
this.btn_div.Size = new System.Drawing.Size(48, 32);
this.btn_div.TabIndex = 16;
this.btn_div.Text = "/";
this.btn_div.Click += new System.EventHandler(this.btn_plus_Click);
//
// btn_abs
//
this.btn_abs.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_abs.Location = new System.Drawing.Point(344, 240);
this.btn_abs.Name = "btn_abs";
this.btn_abs.Size = new System.Drawing.Size(48, 32);
this.btn_abs.TabIndex = 17;
this.btn_abs.Text = "abs";
this.btn_abs.Click += new System.EventHandler(this.btn_abs_Click);
//
// btn_sqrt
//
this.btn_sqrt.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_sqrt.Location = new System.Drawing.Point(344, 184);
this.btn_sqrt.Name = "btn_sqrt";
this.btn_sqrt.Size = new System.Drawing.Size(48, 32);
this.btn_sqrt.TabIndex = 18;
this.btn_sqrt.Text = "sqrt";
this.btn_sqrt.Click += new System.EventHandler(this.btn_abs_Click);
//
// btn_exp
//
this.btn_exp.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_exp.Location = new System.Drawing.Point(344, 128);
this.btn_exp.Name = "btn_exp";
this.btn_exp.Size = new System.Drawing.Size(48, 32);
this.btn_exp.TabIndex = 19;
this.btn_exp.Text = "exp";
this.btn_exp.Click += new System.EventHandler(this.btn_abs_Click);
//
// btn_log
//
this.btn_log.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_log.Location = new System.Drawing.Point(344, 80);
this.btn_log.Name = "btn_log";
this.btn_log.Size = new System.Drawing.Size(48, 32);
this.btn_log.TabIndex = 20;
this.btn_log.Text = "log";
this.btn_log.Click += new System.EventHandler(this.btn_abs_Click);
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("SimSun", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.textBox1.Location = new System.Drawing.Point(136, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 32);
this.textBox1.TabIndex = 23;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(440, 302);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btn_log);
this.Controls.Add(this.btn_exp);
this.Controls.Add(this.btn_sqrt);
this.Controls.Add(this.btn_abs);
this.Controls.Add(this.btn_div);
this.Controls.Add(this.btn_mul);
this.Controls.Add(this.btn_minus);
this.Controls.Add(this.btn_plus);
this.Controls.Add(this.btn_clear);
this.Controls.Add(this.btn_equ);
this.Controls.Add(this.btn_dot);
this.Controls.Add(this.button0);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "计算器";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button0_Click(object sender, System.EventArgs e)
{
if(sender==button0)append_num(0);
if(sender==button1)append_num(1);
if(sender==button2)append_num(2);
if(sender==button3)append_num(3);
if(sender==button4)append_num(4);
if(sender==button5)append_num(5);
if(sender==button6)append_num(6);
if(sender==button7)append_num(7);
if(sender==button8)append_num(8);
if(sender==button9)append_num(9);
}
private void btn_clear_Click(object sender, System.EventArgs e)
{
dblFirst=0.0;
dblSecond=0.0;
blnFirstOper=true;
blnClear=false;
textBox1.Text="";
textBox1.Focus();
}
private void btn_dot_Click(object sender, System.EventArgs e)
{
if(textBox1.Text=="")
textBox1.Text="0";
else
textBox1.Text=textBox1.Text+".";
blnClear=false;
}
private void btn_equ_Click(object sender, System.EventArgs e)
{
if(textBox1.Text!="")
if(blnFirstOper==true)
dblFirst=Convert.ToDouble(textBox1.Text);
else
dblSecond=Convert.ToDouble(textBox1.Text);
switch(strOper)
{
case"+":
dblFirst+=dblSecond;
break;
case"-":
dblFirst-=dblSecond;
break;
case"*":
dblFirst*=dblSecond;
break;
case"/":
dblFirst/=dblSecond;
break;
}
strOper="=";
blnFirstOper=false;
textBox1.Text=Convert.ToString(dblFirst);
blnClear=true;
}
private void btn_plus_Click(object sender, System.EventArgs e)
{
if(textBox1.Text!="")
if(blnFirstOper==true)
dblFirst=Convert.ToDouble(textBox1.Text);
else
dblSecond=Convert.ToDouble(textBox1.Text);
switch(strOper)
{
case"+":
dblFirst+=dblSecond;
break;
case"-":
dblFirst-=dblSecond;
break;
case"*":
dblFirst*=dblSecond;
break;
case"/":
dblFirst/=dblSecond;
break;
}
if(sender==btn_plus)
strOper="+";
else if(sender==btn_minus)
strOper="-";
if(sender==btn_mul)
strOper="*";
if(sender==btn_div)
strOper="/";
blnFirstOper=false;
textBox1.Text=Convert.ToString(dblFirst);
blnClear=true;
}
private void btn_abs_Click(object sender, System.EventArgs e)
{
double tx;
tx=Convert.ToDouble(textBox1.Text);
if(sender==btn_abs)
textBox1.Text=Convert.ToString(Math.Abs(tx));
else if(sender==btn_sqrt)
{
if(tx<0)
MessageBox.Show("负数不能计算平方根");
else
textBox1.Text=Convert.ToString(Math.Sqrt(tx));
}
else if(sender==btn_exp)
textBox1.Text=Convert.ToString(Math.Exp(tx));
else if(sender==btn_log)
{
if(tx<0)
MessageBox.Show("负数不能计算对数");
else
textBox1.Text=Convert.ToString(Math.Log(tx));
}
}
private void append_num(int i)
{
if(blnClear)
{
if(blnFirstOper==true)
dblFirst=Convert.ToDouble(textBox1.Text);
else
dblSecond=Convert.ToDouble(textBox1.Text);
textBox1.Text="";
blnClear=false;
}
if((textBox1.Text!="")||(i!=0))
textBox1.Text=textBox1.Text+i.ToString();
else
textBox1.Text="0";
}
}
}