winform(四)——简单计算器制作

摘要:
渲染:代码区域:usingSystem;使用System.Collections。通用的使用系统。组件模型;使用系统。数据使用系统。绘画使用系统。Linq;使用系统。文本使用System.Threading。任务;使用System.Windows.Fo

效果图:

winform(四)——简单计算器制作第1张

代码区:

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        //存储上次点击了什么按钮,0代表什么都没点击,1代表点击了数字按钮,2代表点击了运算符
        private int prev = 0;
        //存储计算的中间结果
        private decimal zj = 0;
        //记录上次按的什么运算符
        private string prevysf = "+";
        public Form1()
        {
            InitializeComponent();
        }
        //数字键按钮
        private void button8_Click(object sender, EventArgs e)
        {
            //将事件源转换为按钮
            Button btn = sender as Button;
            //替换(如果下面文本框内容为0或者上次点击了运算符)
            if (prev == 2 || txtbottom.Text == "0")
            {
                txtbottom.Text = btn.Text;
            }
            //追加(如果下面文本框内容不为0并且上次没有点击运算符)
            else
            {
                txtbottom.Text += btn.Text;
            }
            //点击了数字按钮
            prev = 1;
        }
        //运算符按钮
        private void button17_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            //上次按了数字
            if (prev == 1)
            {
                txttop.Text += txtbottom.Text + btn.Text;

                switch (prevysf)
                {
                    case "+":
                        zj = zj + Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "-":
                        zj = zj - Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "*":
                        zj = zj * Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "/":
                        zj = zj / Convert.ToDecimal(txtbottom.Text);
                        break;
                }

                txtbottom.Text = zj.ToString(); 
            }
            //上次按了运算符
            else
            {
                string s = txttop.Text;
                s = s.Substring(0, s.Length - 1);
                s = s + btn.Text;
                txttop.Text = s;
            }
            //点击了运算符
            prev = 2;
            //记录下运算符
            prevysf = btn.Text;
          
        }
        //清零按钮
        private void button19_Click(object sender, EventArgs e)
        {
            txttop.Text = "";
            txtbottom.Text = "0";
            prev = 0;
            zj = 0;
            prevysf = "+";
        }

        private void button20_Click(object sender, EventArgs e)
        {
            txtbottom.Text = "0";
        }
        //等号按钮
        private void button4_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            txttop.Text += txtbottom.Text + btn.Text;

            switch (prevysf)
            {
                case "+":
                    zj = zj + Convert.ToDecimal(txtbottom.Text);
                    break;
                case "-":
                    zj = zj - Convert.ToDecimal(txtbottom.Text);
                    break;
                case "*":
                    zj = zj * Convert.ToDecimal(txtbottom.Text);
                    break;
                case "/":
                    zj = zj / Convert.ToDecimal(txtbottom.Text);
                    break;
            }

            txtbottom.Text = zj.ToString();
            txttop.Text = "";
            
            zj = 0;
            
        }
        //
        private void button3_Click(object sender, EventArgs e)
        {
            txtbottom.Text += ".";
        }
    }
}

免责声明:文章转载自《winform(四)——简单计算器制作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Unity3D使用碰撞体做触发器实现简单的自己主动开门以太坊代币合约详析下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【转载】Visual Studio2017如何打包发布Winform窗体程序

在用C#语言编写好Winform窗体程序后,最后一步的操作是将设计好的Winform程序代码进行打包以及发布成安装包。在Visual Studio2017开发工具中,打包发布WinForm程序是比较简单的,只需要简单的几部操作即可完成打包发布操作,此文简要介绍Visual Studio2017打包发布WinForm程序的流程。 (1)首先选中需要打包发布的...

WinForm中DefWndProc、WndProc与IMessageFilter的区别

Windows消息系统由3部分组成:     1.消息队列。Windows应用程序的消息是由Windows统一在一个消息队列中管理的。     2.消息循环。应用程序从Windows消息队列中获得自己的消息,并将其分配给窗口过程进行处理。     3.窗口过程。负责处理接收到的消息,每个窗口都有对应的窗口过程,负责截获消息并响应。WndProc是窗口过程函...

C# winform DataGridView 常见属性

C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行、列的隐藏和删除 ⑦ 禁止列或者行的Resize ⑧ 列宽和行高以及列头的高度和行头的宽度的自动调整 ⑨ 冻结列或行 ⑩ 列顺序的调整 ⑪ 行头列头的单元格⑫ 剪切板的操作...

C#中对Winform中的DataGridView的控制技巧。(单独控制某单元格的按钮不显示、某单元格的ReadOnly)

1:控制按钮列中的某一行不显示按钮。(使用环境:数据的移动,如把第二行的数据移动到上面去,最下面的一行为合计行,不允许移动,因此,就需要把第一行与最后一行的按钮屏蔽掉。屏蔽的方法:把按钮改成普通的单元格。具体实现代码如下:(第8列中的第一行与最后一行按钮不显示)) //定义绘画表格前的事件,在绘画前把按钮转换成普通单元格。dataGrid.RowPreP...

微软:正式发布针对 .NET Core的 Winform 设计器

转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 原文出处:https://devblogs.microsoft.com/dotnet/windows-forms-designer-for-net-core-released/ 作为微软最引以为傲的IDE编辑器,Visual Studio已经让无数专注于 .NE...

winform(三)——更换主窗体例子

做一个登录窗口,登录成功时关闭form1,展示from2界面 1.主界面Login namespace WindowsFormsApplication1 { public partial class Login : Form { public string username;//定义一个变量给子窗体传值...