using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
double result;
int r;
//声明
public Form1()
{
InitializeComponent();
}
//双击button2后添加代码
private void button2_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 1.ToString();
}
//双击button1后添加代码
private void button1_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 0.ToString();
}
//双击button3后添加代码
private void button3_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 2.ToString();
}
//双击button4后添加代码
private void button4_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 3.ToString();
}
//双击button5后添加代码
private void button5_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 4.ToString();
}
//双击button6后添加代码,以后的操作类似
private void button6_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 5.ToString();
}
private void button7_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 6.ToString();
}
private void button8_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 7.ToString();
}
private void button9_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 8.ToString();
}
private void button10_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + 9.ToString();
}
private void bDot_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + ".".ToString();
}
private void bClr_Click(object sender, EventArgs e)
{
txtCalc.Text ="";
}
private void bPlus_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + "+";
}
private void bEqu_Click(object sender, EventArgs e)
{
if (txtCalc.Text.IndexOf("+") != -1 && txtCalc.Text.IndexOf("+") != 0)
{
r = txtCalc.Text.IndexOf("+");
result = double.Parse(txtCalc.Text.Substring(0, r)) + double.Parse(txtCalc.Text.Substring(r + 1));
txtCalc.Text = result.ToString();
}
else if (txtCalc.Text.IndexOf("-") != -1 && txtCalc.Text.IndexOf("+") != 0)
{
r = txtCalc.Text.IndexOf("-");
result = double.Parse(txtCalc.Text.Substring(0, r)) - double.Parse(txtCalc.Text.Substring(r + 1));
txtCalc.Text = result.ToString();
}
else if (txtCalc.Text.IndexOf("*") != -1 && txtCalc.Text.IndexOf("+") != 0)
{
r = txtCalc.Text.IndexOf("*");
result = double.Parse(txtCalc.Text.Substring(0, r)) * double.Parse(txtCalc.Text.Substring(r + 1));
txtCalc.Text = result.ToString();
}
else if (txtCalc.Text.IndexOf("/") != -1 && txtCalc.Text.IndexOf("+") != 0)
{
r = txtCalc.Text.IndexOf("/");
result = double.Parse(txtCalc.Text.Substring(0, r)) / double.Parse(txtCalc.Text.Substring(r + 1));
txtCalc.Text = result.ToString();
}
else
{
MessageBox.Show("请输入正确数字", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void bMul_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + "*";
}
private void bDiv_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + "/";
}
//时间
private void button11_Click(object sender, EventArgs e)
{
txtCalc.Text =DateTime.Now.Year+"年"+DateTime.Now.Month+"月"+DateTime.Now.Day+"日"+DateTime.Now.Hour+"时"
+DateTime.Now.Minute+"分"+DateTime.Now.Second+"秒";
}
private void button12_Click(object sender, EventArgs e)
{
txtCalc.Text =" 欢迎使用!";
}
private void bSub_Click(object sender, EventArgs e)
{
txtCalc.Text = txtCalc.Text + "-";
}
转载于:https://blog.51cto.com/2503461/437701