Winfrom编写简单的计算器

本文介绍了如何使用Winform创建一个基础的计算器应用,涵盖了界面设计和基本计算逻辑的实现。
摘要由CSDN通过智能技术生成

简单的计算器

基本布局:


代码:
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 Calculator
{
    public partial class calculator : Form
    {
        public calculator()
        {
            InitializeComponent();
            lb_1.Text = null;
        }


        private void calculator_Load(object sender, EventArgs e)
        {


        }


        private string FuHao;
        private float? Mun;
        private float? Num;
        private bool IsCounting = false;
        private bool IsHaving = false;
        private bool IsJiSuan = false;


        private void ShuRu(string shu)
        {
            if(!IsCounting)
            {
                txtShuchu.Text = txtShuchu.Text + shu;
            }
            else
            {
                txtShuchu.Text = "";
           
好的,以下是一个简单的 WinForms 计算器的示例代码,你可以在 Visual Studio 中创建一个新的 WinForms 项目并将代码复制到 Form1.cs 文件中: ```csharp using System; using System.Windows.Forms; namespace WinFormsCalculator { public partial class Form1 : Form { private double result = 0; private string operation = ""; private bool operation_pressed = false; public Form1() { InitializeComponent(); } private void button_Click(object sender, EventArgs e) { if ((textBox_Result.Text == "0") || (operation_pressed)) textBox_Result.Clear(); operation_pressed = false; Button b = (Button)sender; textBox_Result.Text = textBox_Result.Text + b.Text; } private void operator_Click(object sender, EventArgs e) { Button b = (Button)sender; operation = b.Text; result = double.Parse(textBox_Result.Text); label_CurrentOperation.Text = result + " " + operation; operation_pressed = true; } private void button_ClearEntry_Click(object sender, EventArgs e) { textBox_Result.Text = "0"; } private void button_Clear_Click(object sender, EventArgs e) { textBox_Result.Text = "0"; result = 0; } private void button_Equals_Click(object sender, EventArgs e) { switch (operation) { case "+": textBox_Result.Text = (result + double.Parse(textBox_Result.Text)).ToString(); break; case "-": textBox_Result.Text = (result - double.Parse(textBox_Result.Text)).ToString(); break; case "*": textBox_Result.Text = (result * double.Parse(textBox_Result.Text)).ToString(); break; case "/": textBox_Result.Text = (result / double.Parse(textBox_Result.Text)).ToString(); break; default: break; } result = double.Parse(textBox_Result.Text); label_CurrentOperation.Text = ""; } } } ``` 在此代码中,我们使用了一个 `result` 变量来存储当前的计算结果,`operation` 变量来存储当前的操作符,以及一个 `operation_pressed` 变量来记录上一个按键是否为操作符。我们还定义了几个事件处理程序来处理按钮点击事件和清空操作。 在窗体设计器中,我们可以将所有数字按钮和操作符按钮的 `Click` 事件绑定到 `button_Click` 和 `operator_Click` 方法上,将 "CE" 和 "C" 按钮的 `Click` 事件绑定到 `button_ClearEntry_Click` 和 `button_Clear_Click` 方法上,将 "等于" 按钮的 `Click` 事件绑定到 `button_Equals_Click` 方法上。 最后,在窗体设计器中添加一个 `TextBox` 控件来显示计算结果,还可以添加一个 `Label` 控件来显示当前的操作符。运行该应用程序后,你应该就可以使用这个简单的 WinForms 计算器了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值