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();
}
/*1.显示屏
* 2.0-9数字按钮和+-×÷=按钮计算器
* 3.实现输入第一个数字
* 4.输入运算符号
* 5.输入第二个数字
* 6.点击=计算出结果,结果显示在显示屏上
*/
//随机对象实例化
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循环找到所有的button按钮
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;
//定义一个值来确定文本框2是否按下等号
bool dy = false;
//1
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;
}
//2
private void button2_Click(object sender, EventArgs e)
{
button2.BackColor = Color.FromArgb
c#.net之窗体应用程序按钮版计算器
最新推荐文章于 2024-06-20 15:10:44 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)