数据库作业2——利用c#设计可视化简易计算器 (加减乘除)
在vs里新建项目,找到c#中的Windows窗体应用,命名即可。
如下:
在视图里打开工具箱:也可用快捷键(Ctrl+Alt+x)
注:打开后工具箱就固定在左侧,再选工具时直接点击即可.
在这里我们只需用到如下两种工具:可以拖动至设计器也可双击
构建大致界面:右键按钮选属性可以调节字体颜色及大小
双击按钮编码,代码如下:
using System;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class 计算器 : Form
{
public 计算器()
{
InitializeComponent();
}
double a, b, c;//两个预置数,a、b暂存,c作为结果输出
string f;//一个标识符用于判断+-*/
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "1";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button9_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "2";
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "3";
}
private void button8_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "4";
}
private void button7_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "6";
}
private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "7";
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "8";
}
private void button11_Click(object sender, EventArgs e)
{
b = Convert.ToDouble(textBox1.Text);
if(f=="+")
c = a + b;
if (f == "-")
c = a -b;
if (f == "*")
c = a *b;
if (f == "/")
c = a /b;
textBox1.Text = c.ToString();
}
private void button15_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
}
private void button12_Click(object sender, EventArgs e)
{
a = Convert.ToDouble(textBox1.Text);
f = "-";
textBox1.Text = "";
}
private void button13_Click(object sender, EventArgs e)
{
a = Convert.ToDouble(textBox1.Text);
f = "*";
textBox1.Text = "";
}
private void button14_Click(object sender, EventArgs e)
{
a = Convert.ToDouble(textBox1.Text);
f = "/";
textBox1.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "";
textBox1.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
a=Convert.ToDouble(textBox1.Text) ;
f = "+";
textBox1.Text = "";
}
}
}
注意连按数字的情况所以代码应为 textBox1.Text += “1”;
清零以后再其他按数字之前要先把0去掉,所以在按数字时需判断一下输入框是否为0