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();
}
Random si = new Random();
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
textBox1.BackColor = Color.Pink;
textBox1.ForeColor = Color.White;
textBox1.Font = new Font("",30);
textBox1.ReadOnly = true;
textBox2.Font = new Font("", 30);
textBox2.ReadOnly = true;
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
{
Button btn = (Button)ctl;
btn.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
btn.ForeColor = Color.White;
btn.Font = new Font("", 20);
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderSize = 0;
}
}
}
double num1;
bool isCala = false;
bool isdian = false;
string type;
bool dy = false;
private void button1_Click(object sender, EventArgs e)
{
button1.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy==true)
{
textBox1.Text = "";
textBox2.Text = "";
dy= false;
}
if (isCala==true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 1;
textBox2.Text += 1;
}
private void button2_Click(object sender, EventArgs e)
{
button2.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 2;
textBox2.Text += 2;
}
private void button3_Click(object sender, EventArgs e)
{
button3.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 3;
textBox2.Text += 3;
}
private void button4_Click(object sender, EventArgs e)
{
button4.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 4;
textBox2.Text += 4;
}
private void button5_Click(object sender, EventArgs e)
{
button5.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 5;
textBox2.Text += 5;
}
private void button6_Click(object sender, EventArgs e)
{
button6.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 6;
textBox2.Text += 6;
}
private void button7_Click(object sender, EventArgs e)
{
button7.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 7;
textBox2.Text += 7;
}
private void button8_Click(object sender, EventArgs e)
{
button8.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 8;
textBox2.Text += 8;
}
private void button9_Click(object sender, EventArgs e)
{
button9.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 9;
textBox2.Text += 9;
}
private void button10_Click(object sender, EventArgs e)
{
button10.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isCala == true)
{
isCala = false;
textBox1.Text = "";
}
textBox1.Text += 0;
textBox2.Text += 0;
}
private void button16_Click(object sender, EventArgs e)
{
button16.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
if (dy == true)
{
textBox1.Text = "";
textBox2.Text = "";
dy = false;
}
if (isdian==false)
{
textBox1.Text += ".";
textBox2.Text += ".";
isdian = true;
}
}
private void button11_Click(object sender, EventArgs e)
{
type = "+";
if (!double.TryParse(textBox1.Text,out num1))
{
return;
}
else
{
num1 = double.Parse(textBox1.Text);
}
isCala = true;
isdian = false;
textBox2.Text += "+";
button11.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
}
private void button12_Click(object sender, EventArgs e)
{
type = "-";
if (!double.TryParse(textBox1.Text, out num1))
{
return;
}
else
{
num1 = double.Parse(textBox1.Text);
}
isCala = true;
isdian = false;
textBox2.Text += "-";
button12.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
}
private void button13_Click(object sender, EventArgs e)
{
type = "*";
if (!double.TryParse(textBox1.Text, out num1))
{
return;
}
else
{
num1 = double.Parse(textBox1.Text);
}
isCala = true;
isdian = false;
textBox2.Text += "*";
button13.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
}
private void button14_Click(object sender, EventArgs e)
{
type = "/";
if (!double.TryParse(textBox1.Text, out num1))
{
return;
}
else
{
num1 = double.Parse(textBox1.Text);
}
isCala = true;
isdian = false;
textBox2.Text += "/";
button14.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
}
double num2;
private void button15_Click(object sender, EventArgs e)
{
if (!double.TryParse(textBox1.Text,out num2))
{
return;
}
else
{
num2 = double.Parse(textBox1.Text);
}
button15.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
isCala = true;
dy = true;
if (type=="+")
{
textBox1.Text = (num1 + num2).ToString();
}
if (type == "-")
{
textBox1.Text = (num1 - num2).ToString();
}
if (type == "*")
{
textBox1.Text = (num1 * num2).ToString();
}
if (type == "/")
{
textBox1.Text = (num1 / num2).ToString();
}
textBox2.Text +="="+ textBox1.Text;
}
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
button17.BackColor = Color.FromArgb(si.Next(256), si.Next(256), si.Next(256));
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void button18_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length>0)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
if (textBox2.Text.Length > 0)
{
textBox2.Text = textBox1.Text.Substring(0, textBox2.Text.Length - 1);
}
}
}
}