C#实现计算器功能(winform版)
作者:Dust_SongYunfei 发布时间:2021-07-16 06:40:15
标签:C#,计算器
本文实例为大家分享了C#实现计算器功能的具体代码,供大家参考,具体内容如下
代码:
Random rad = new Random(); // 实例化随机对象
private void Form1_Load(object sender, EventArgs e)
{
this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2;
this.Text = "计算器";
textBox1.ReadOnly =true;// 文本框无法输入字符
foreach (Control ctl in this.Controls)
{ // 获取所有按钮 改变背景颜色和数字和符号颜色
if (ctl is Button)
{
Button btn = ctl as Button;
btn.BackColor = Color.FromArgb(rad.Next(256),rad.Next(256),rad.Next(256),rad.Next(256));
btn.ForeColor = Color.FromArgb(rad.Next(256), rad.Next(256), rad.Next(256));
}
}
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();// 关闭窗体
}
char fuhao;// 接收符号
double temp, num; // temp第一个值,num为第二个值
// 1
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
// 2
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
// 3
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
// 4
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
// 5
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
// 6
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
// 7
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
// 8
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
// 9
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
// 0
private void button15_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
//点
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
// +
private void button4_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
fuhao = '+';
textBox1.Text += "+";
//textBox1.Text = null;
}
// -
private void button8_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
fuhao = '-';
textBox1.Text = null;
}
// ×
private void button12_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
fuhao = '×';
textBox1.Text = null;
}
// ÷
private void button20_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
fuhao = '÷';
textBox1.Text = null;
}
// %
private void button19_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
fuhao = '%';
textBox1.Text = null;
}
// =
private void button13_Click(object sender, EventArgs e)
{
temp = double.Parse(textBox1.Text);
switch (fuhao)
{
case '+':
num += temp;
break;
case '-':
num -= temp;
break;
case '×':
num *= temp;
break;
case '÷':
num /= temp;
break;
case '%':
num %= temp;
break;
default:
break;
}
textBox1.Text = num.ToString();
fuhao = ' ';
num = 0;
temp = 0;
}
// 删除
private void button18_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
}
//清空
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
来源:https://blog.csdn.net/dust__/article/details/103076040


猜你喜欢
- 注:完整项目下载在处理了核心任务之后,我们会发现有些请求并不是都是静态的,那么我们就需要进行实现处理动态请求的要求,如下面代码是我们请求的解
- 前言代码库:https://gitee.com/leo825/springboot-learning-parents.git之前写过《Spr
- 一、实验目的1. 掌握输入输出流的总体结构;2. 掌握流的概念;3. 掌握FileInputStream类、FileOutputStream
- 1、代码就一个Controller,从官网复制过来的,如下package com.springboot.controller;import
- 前面一篇有说道如何在MyEclipse中搭建maven项目,这里将继续介绍如何在搭建好的基础maven项目中引入我们常用的javaweb框架
- 最近单位又有一个新Java项目。涉及到扫码登录。之前项目使用的是 ajax轮询的方式。感觉太low了。所以这次用webSocket的方式进行
- Java Config 下的Spring Test方式用了三种方式:1.纯手动取bean:package com.wang.test;imp
- 在传统的Java编程中,被广为人知的一个知识点是:java Interface接口中不能定义private私有方法。只允许我们定义publi
- 关联篇:HandlerThread 使用及其源码完全解析关联篇:Handler内存泄漏详解及其解决方案一说到Android的消息机制,自然就
- 布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去
- Java BigDecimal的坑采坑处 BigDecimal bd =new BigDecimal(0.1);
- 本文实例讲述了C#实现查杀本地与远程进程的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.
- 一、什么是锁擦除锁擦除是指虚拟机即时编译器(JIT)在运行时,对一些代码上要求同步,但是被检测到不可能存在共享数据竞争的锁进行擦除。锁擦除的
- 看完了前面说的几本书
- 断断续续的总算的把android开发和逆向
- 第一种方法这种方法需要配置 hibernate.cfg.xml 的属性 hibernate.hbm2ddl.auto,该属性值的具体说明如下
- 一、类成员的访问级别public:可由任何代码访问。private(默认):只能由类中的代码访问。internal:只能由它所在的项目(程序
- 1. 启动入口本系列RocketMQ4.8注释github地址,希望对大家有所帮助,要是觉得可以的话麻烦给点一下Star哈前面我们已经分析完
- 本文实例讲述了Java基于正则实现的日期校验功能。分享给大家供大家参考,具体如下:private void checkDate() thro
- 就是每隔一定的时间显示一张图片,全部图片文件位于:“工作空间\项目名称\bin\动态图\花好月圆\”文件夹下。文件名类似:1001.jpg,