1:首先,打开Visual Stdio
2:创建如图选择的项目
3:取个名字,点创建
4:稍等片刻就可以如图所示进行规划自己计算器的位置了,我只用到的Button和TextBox
5:右击Botton后选择属性
可以对其进行属性设置,简单点的话如下两图所示
7:然后进行对按键进行内部程序设计
8:下面的有点多,不着急哈,大部分都是差不多的,可以稍微看看中间部分的,理解机理就so easy
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double Lnum, Rnum, Res;
String Flag;
int i=0;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button13_Click(object sender, EventArgs e) //按键.
{
if (i == 0 ){
textBox1.Text += ".";
i=1;
}
}
private void button1_Click(object sender, EventArgs e) //按键1
{
textBox1.Text += "1";
}
private void button2_Click(object sender, EventArgs e) //按键2
{
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e) //按键3
{
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e) //按键4
{
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e) //按键5
{
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e) //按键6
{
textBox1.Text += "6";
}
private void button7_Click(object sender, EventArgs e) //按键7
{
textBox1.Text += "7";
}
private void button8_Click(object sender, EventArgs e) //按键8
{
textBox1.Text += "8";
}
private void button9_Click(object sender, EventArgs e) //按键9
{
textBox1.Text += "9";
}
private void button11_Click(object sender, EventArgs e) //进行+法
{
Lnum = Convert.ToDouble(textBox1.Text);
Flag = "+";
textBox1.Text = "";
i=0;
}
private void button14_Click(object sender, EventArgs e) //等号公式
{
Rnum = Convert.ToDouble(textBox1.Text);
if (Flag == "+") Res = Lnum + Rnum;
else if (Flag == "-") Res = Lnum - Rnum;
else if (Flag == "*") Res = Lnum * Rnum;
else if(Flag == "/") Res = Lnum / Rnum;
else {
Lnum = Convert.ToDouble(textBox1.Text);
Res = Lnum;
}
textBox1.Text = Res.ToString() ;
}
private void button10_Click(object sender, EventArgs e)//进行-法
{
Lnum = Convert.ToDouble(textBox1.Text);
Flag = "-";
textBox1.Text = "";
i=0;
}
private void button16_Click(object sender, EventArgs e)//进行*法
{
Lnum = Convert.ToDouble(textBox1.Text);
Flag = "*";
textBox1.Text = "";
i=0;
}
private void button12_Click(object sender, EventArgs e)//归零
{
Lnum = Rnum = 0;
textBox1.Text = "";
}
private void button15_Click(object sender, EventArgs e)//进行/法
{
Lnum = Convert.ToDouble(textBox1.Text);
Flag = "/";
textBox1.Text = "";
i=0;
}
private void button17_Click(object sender, EventArgs e)//按键0
{
textBox1.Text += "0";
}
}
}
上边的很好理解,注意一下区别,大部分都是CTRL+C,CTRL+V来的,挺简单的…
然后分析一下以上的问题:
1、对于清0(“C”)选项,清0之后会无数,也就是无法进行计算
2、目前对于括号和加减乘除的先后顺序需要进行改进
3、功能不够齐全
等我再想想,慢慢来吧,以上进行一下简单的计算任务是完全么的问题的…