C#实现简单的计算器小功能
作者:我自是年少韶华倾负 时间:2023-05-13 06:46:11
本文实例为大家分享了C#实现简单的计算器小功能的具体代码,供大家参考,具体内容如下
先来张效果图吧(5分钟写好,莫怪)
代码:
数字按钮绑定的是button_Clickd()方法
运算符按钮绑的是Button_Clickp()方法
思想:按下数字按钮,将数字按钮的值连接到textbox上,然后按下运算符判断是否为等于,并记录运算符的内容,以便后面做处理。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form2 : Form
{
private string s;
private double x, y;
private Button btn;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = "";
label1.Text="";
}
private void buttond_Click(object sender, EventArgs e)//数字符所绑定的事件
{
btn = (Button)sender;
textBox1.Text = textBox1.Text + btn.Text;//将所点击的数字付呈现在textBox上面
}
private void buttonp_Click(object sender, EventArgs e)//运算符所绑定的事件
{
btn = (Button)sender;
if (btn.Name != "button12")//如果不是"="
{
x = Convert.ToDouble(textBox1.Text);//将所所输入的第一个字符保留下来
textBox1.Text = "";//清空textBox的内容
s = btn.Name;//获取运算符的种类
label1.Text = x.ToString();//将第一个所按的字符输出来
}
else
{
if (label1.Text == "")
MessageBox.Show("输入不正确!!", "信息提示", MessageBoxButtons.OK);
else
{
y = Convert.ToDouble(textBox1.Text);
switch (s)//使用s来判读所按的按钮
{
case "button13":
textBox1.Text = (x + y).ToString();
break;
case "button14":
textBox1.Text = (x - y).ToString();
break;
case "button15":
textBox1.Text = (x * y).ToString();
break;
case "button16":
if (y == 0)
MessageBox.Show("除零错误!!!", "信息提示", MessageBoxButtons.OK);
else
textBox1.Text = (x / y).ToString();
break;
}
label1.Text = textBox1.Text;
}
}
}
}
}
来源:https://blog.csdn.net/qq_38345598/article/details/79561013
标签:C#,计算器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis-Plus中最简单的查询操作教程(Lambda)
2022-03-16 13:43:28
![](https://img.aspxhome.com/file/2023/7/73097_0s.png)
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)
2022-11-14 18:37:22
![](https://img.aspxhome.com/file/2023/4/65724_0s.png)
MybatisPlus使用@TableId主键id自增长无效的解决
2023-01-30 15:59:41
![](https://img.aspxhome.com/file/2023/4/75024_0s.png)
Java中反射的学习笔记分享
2021-12-18 14:41:43
![](https://img.aspxhome.com/file/2023/8/86828_0s.jpg)
Android输入法弹出时覆盖输入框问题的解决方法
2023-02-06 06:36:52
C#条件拼接Expression<Func<T, bool>>的使用
2023-07-11 22:29:18
springboot整合security和vue的实践
2021-09-17 20:39:28
![](https://img.aspxhome.com/file/2023/5/97985_0s.jpg)
深入了解Java中String、Char和Int之间的相互转换
2022-09-14 10:18:54
![](https://img.aspxhome.com/file/2023/2/64382_0s.jpg)
java 请求跨域问题解决方法实例详解
2023-08-24 02:55:11
Maven如何打入依赖中指定的部分jar包
2023-09-22 02:50:33
C++实现的求解多元一次方程示例
2022-09-26 19:45:47
详谈java命令的本质逻辑揭秘
2022-03-02 05:26:32
Java编程实现月食简单代码分享
2022-12-27 12:33:24
![](https://img.aspxhome.com/file/2023/3/79943_0s.png)
Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用示例
2022-12-07 07:30:59
![](https://img.aspxhome.com/file/2023/5/87295_0s.jpg)
Java如何通过枚举实现有限状态机
2021-08-05 04:21:05
Spring Core动态代理的实现代码
2021-12-11 03:40:54
Android判断包名和类名是否存在的方法
2021-09-23 02:52:52
详解Java中的Lambda表达式
2023-06-05 01:37:16
基于Java编写串口通信工具
2022-11-30 09:25:34
![](https://img.aspxhome.com/file/2023/9/93069_0s.jpg)
JVM致命错误日志详解(最新推荐)
2023-08-22 07:42:30