吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4439|回复: 12
收起左侧

[其他原创] 初识WinForm之自制计算器

  [复制链接]
wuaixql 发表于 2017-12-16 11:54
本帖最后由 wuaixql 于 2017-12-16 11:55 编辑

昨天刚刚学了C#的WinForm窗口程序,就自己尝试去做了一个计算器,先上图
QQ截图20171216115211.png     界面是丑了一点,哈哈!     大牛勿喷,代码奉上,喜欢的童鞋可以拿去研究,其实挺简单的




[C#] 纯文本查看 复制代码
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 JSQ
{
    public partial class Frmjsq : Form
    {
        public Frmjsq()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "1";
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "2";
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "3";
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "4";
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "5";
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "6";
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "7";
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "8";
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "9";
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "0";
        }

        private void btnjia_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "+";
        }

        private void btnjian_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "-";
        }

        private void btncheng_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "*";
        }

        private void btnchu_Click(object sender, EventArgs e)
        {
            string n = txtShow.Text;
            txtShow.Text = n + "/";
        }

        private void btndy_Click(object sender, EventArgs e)
        {
            string nums = txtShow.Text;
            int fuhaowz = 0;
            if ( nums[nums.Length - 1] == '+' ||
                nums[nums.Length - 1] == '-' ||nums[nums.Length - 1] == '*'
                || nums[nums.Length - 1] == '/')
            {
                MessageBox.Show("错误");
                txtShow.Text = null;
            }
            else { 
            char[] fuhao = { '+', '-', '*', '/' };
            for (int i = 0; i < nums.Length; i++)
            {
                for (int j = 0; j < fuhao.Length; j++)
                {
                    if (fuhao[j] == nums[i])
                    {
                         fuhaowz = i;
                        break;
                    }
                }
            }
            char b = nums[fuhaowz];
           string[]m= nums.Split(b);
            double n1 = Double.Parse(m[0]);
            double n2 = Double.Parse(m[1]);
            switch (b)
            {
                case '+':
                    txtShow.Text =( n1 + n2).ToString();
                    break;
                case '-':
                    txtShow.Text = (n1 - n2).ToString();
                    break;
                case '*':
                    txtShow.Text = (n1 * n2).ToString();
                    break;
                case '/':
                    txtShow.Text = (n1 / n2).ToString();
                    break;
            }
            }
            //for (int i = 0; i < nums.Length; i++)
            //{

            //    else
            //    {
            //        if (nums[i] == '+' || nums[0] == '-' || nums[0] == '*' || nums[0] == '/')
            //        {
            //            fuhao = nums[i].ToString();
            //        }
            //        else
            //        {

            //            if (nums[i] == '+' || nums[0] == '-' || nums[0] == '*' || nums[0] == '/')
            //            {
            //                fuhao = nums[i].ToString();
            //            }
            //        }
            //    }

            //} 
        }

        private void btnqc_Click(object sender, EventArgs e)
        {
            txtShow.Text = null;
        }
    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| wuaixql 发表于 2017-12-17 09:47
不会你举啥手! 发表于 2017-12-16 13:43
能做出个大概就是好样的,楼主继续加油

谢谢,就是还有好多漏洞,目前的我还是无法解决
陌生城市 发表于 2017-12-16 12:08 来自手机
颜笑 发表于 2017-12-16 12:12 来自手机
wwlychan 发表于 2017-12-16 12:18
ddddddddd
dazhuang 发表于 2017-12-16 13:21
那个大学的
wei692017 发表于 2017-12-16 13:36
这个是搞IT开发的人用的吧?
不会你举啥手! 发表于 2017-12-16 13:43
能做出个大概就是好样的,楼主继续加油
Seven_2017 发表于 2017-12-16 13:51
不错的   很不错
刘统宝 发表于 2017-12-16 14:04
能来点语音吗
头像被屏蔽
baixingjian8 发表于 2017-12-16 18:03
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-15 10:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表