網站首頁 編程語言 正文
本文實例為大家分享了C#實現簡單的計算器功能的具體代碼,供大家參考,具體內容如下
環境:VS2010及以上版本
1、建立個Window窗體應用
2、在工具箱里拖出兩個TextBox,第一個放上面,第二個放下面 。主要這里的Name,上面是textBox1,下面是textBox2。這涉及到后面代碼的書寫
3、在工具欄里拖動Button,擺放好??衫蒙厦娴膶R工具輔助設計。
4、在屬性里改變各Button的Text,如下
注意這里的1~9,小數點,±*/ 的Text應只有一個字符,不要多輸?!?/p>
5、選中任意一個Button,右鍵,選擇查看代碼,轉到Form1.cs
6、開始寫代碼
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(); ? ? ? ? } ? ? } }
7、設置各Button的Click事件
AddNum: 1~9與小數點的Click事件
Reset:CE的Click事件
Delete:←的Click事件
Calculate :±*/的Click事件
Equal:= 的Click事件
8、啟動(F5)
原文鏈接:https://blog.csdn.net/weixin_46068322/article/details/114920031
相關推薦
- 2022-06-06 微信小程序實現滾動視圖點擊錨點跳轉、點擊左側分欄時右側對應內容置頂、左右分欄聯動、setTimeou
- 2022-10-16 QT網絡通信TCP客戶端實現詳解_C 語言
- 2022-03-31 nginx平滑重啟和平滑升級的圖文教程_nginx
- 2023-04-19 nginx: [error] CreateFile() “D:\nginx-1.21.6/logs/
- 2023-11-25 消息的訂閱與發布機制
- 2024-04-08 SpringBoot緩存注解@Cacheable、@CacheEvict和@CachePut
- 2022-06-20 Flutter路由守衛攔截的實現_Android
- 2022-10-22 Python?修改CSV文件實例詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支