当前位置:网站首页>C#实现简单的计算器
C#实现简单的计算器
2022-08-02 14:12:00 【夏湾】
C#实现简单的计算器
环境:VS2010及以上版本
- 建立个Window窗体应用

- 在工具箱里拖出两个TextBox,第一个放上面,第二个放下面 。主要这里的Name,上面是textBox1,下面是textBox2。这涉及到后面代码的书写

- 在工具栏里拖动Button,摆放好。可利用上面的对齐工具辅助设计。

- 在属性里改变各Button的Text,如下

注意这里的1~9,小数点,±*/ 的Text应只有一个字符,不要多输。← - 选中任意一个Button,右键,选择查看代码,转到Form1.cs

- 开始写代码
- AddNum 修改TextBox的Text,应用于1~9与小数点的Click事件
- Reset 重置temp、myoperator,以及两个TextBox的Text
- Delete 删除textBox2的Text最后一个字符
- Calculate 把textBox2的Text转为double给temp,修改myoperator
- Equal 具体的计算
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//----上面是自动生成的代码,下面得我们手写----
private double temp = 0; //存储临时数据
private char myoperator = ' '; //判断之前按的是+-*/中的哪个
private void AddNum(object sender, EventArgs e)
{ // 1~9与小数点的Click事件
//sender是引发该事件的控件,这里我们拆箱为Button
Button button = (Button)sender;
textBox2.Text += button.Text;
}
private void Reset(object sender, EventArgs e)
{ // CE的Click事件
temp = 0;
myoperator = ' ';
textBox1.Text = textBox2.Text = "";
}
private void Delete(object sender, EventArgs e)
{ // ←的Click事件
//移除最后一个字符
if (textBox2.TextLength > 0)
textBox2.Text = textBox2.Text.Remove(textBox2.TextLength - 1);
}
private void Calculate(object sender, EventArgs e)
{ // +-*/的Click事件
Button button = (Button)sender;
if (double.TryParse(textBox2.Text, out temp)) //尝试把textBox2的Text转为double并赋值给temp
{
myoperator = button.Text[0]; //Text是string,取第一个字符
textBox1.Text = temp.ToString() + ' ' + myoperator;
textBox2.Text = "";
}
else
{ //转换失败,重置所有
Reset(sender, e);
}
}
private void Equal(object sender, EventArgs e)
{ // = 的Click事件,计算并显示
double temp2;
//尝试转换,失败则重置并返回
if (!double.TryParse(textBox2.Text, out temp2)) { Reset(sender, e); return; }
switch (myoperator)
{
case '+':
temp += temp2;
break;
case '-':
temp -= temp2;
break;
case '*':
temp *= temp2;
break;
case '/':
temp /= temp2;
break;
default:
break;
}
textBox1.Text = "";
textBox2.Text = temp.ToString();
}
}
}
- 设置各Button的Click事件
- AddNum: 1~9与小数点的Click事件
- Reset:CE的Click事件
- Delete:←的Click事件
- Calculate :±*/的Click事件
- Equal:= 的Click事件

- 启动(F5)
边栏推荐
- MATLAB绘制平面填充图入门详解
- unity Domain Reload & scene Reload 静态变量重置
- 4. Publish Posts, Comment on Posts
- 4.发布帖子,评论帖子
- Based on the matrix calculation in the linear regression equation of the coefficient estimates
- 求解斐波那契数列的若干方法
- 二叉树的遍历:递归法/ 迭代法/ 统一迭代法(强QAQ)
- 7. Redis
- GMP scheduling model of golang
- Codeforces Round #605 (Div. 3)
猜你喜欢

队列与栈

倍增和稀疏表

unity-shader(入门)

word方框怎么打勾?

STM32LL library use - SPI communication
![[STM32 Learning 1] Basic knowledge and concepts are clear](/img/1c/7c4fd2d835e15ca13517c6d97e9b3a.png)
[STM32 Learning 1] Basic knowledge and concepts are clear

cmake configure libtorch error Failed to compute shorthash for libnvrtc.so

STM32LL库——USART中断接收不定长信息

MATLAB绘图函数fplot详解

Based on the least squares linear regression equation coefficient estimation
随机推荐
第二十五章:一文掌握while循环
第三十三章:图的基本概念与性质
软件测试基础知识(背)
C语言函数参数传递模式入门详解
计算机导论——数据库
永久更改pip源
LeetCode 2343. 裁剪数字后查询第 K 小的数字 暴力+语法考察
Test case exercises
线性结构,顺序结构
3.用户上传头像
Unity插件-NGUI
2342. 数位和相等数对的最大和 哈希优化
IPV4和IPV6是什么?
IDEA 单元测试报错:Class not found
Detailed introduction to the hierarchical method of binary tree creation
剑指offer:删除链表中重复的节点
pygame拖动条的实现方法
pygame图像连续旋转
Network Security Packet Capture
Open the door of electricity "Circuit" (1): voltage, current, reference direction