您当前的位置: 首页 >  .net

川川菜鸟

暂无认证

  • 2浏览

    0关注

    969博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

web信息管理课程(3):ASP.NET编写简单的加减法计算器

川川菜鸟 发布时间:2022-04-12 22:09:23 ,浏览量:2

控件设置
  1. 拖动textbutton控件,用于显示
  2. 拖动Button控件,组成数字和符号键盘,
控件属性名属性值说明TextBoxIDtxtDisplay用于显示输入数字的文本框控件编程名称ReadOnlyTrue不能更改文本框中的文本,默认值为FalseBotton分别设置ID和Text分别设置为btnOne和数字1分别表示 “数字1”按钮的编程名称和“数字1”按钮上显示的文本Botton分别设置ID和Text分别设置为btnTwo和数字2分别表示 “数字2”按钮的编程名称和“数字2”按钮上显示的文本Botton分别设置ID和Text分别设置为btnThree和数字3分别表示 “数字3”按钮的编程名称和“数字3”按钮上显示的文本Botton分别设置ID和Text分别设置为btnAdd和符号“+"分别表示 “+”按钮的编程名称和“+”按钮上显示的文本Botton分别设置ID和Text分别设置为btnSubtract和符号“-"分别表示 “+”按钮的编程名称和“-”按钮上显示的文本Botton分别设置ID和Text分别设置为btnEqual和符号“="分别表示 “=”按钮的编程名称和“=”按钮上显示的文本

添加好后可以手动拖动控件调整大小,效果如下: 在这里插入图片描述

form标签源码为:


        
            简易计算器
            
            
            
            
            
            
            
        
    
编写计算代码
 public partial class WebForm1 : System.Web.UI.Page
    {
        static string num1 = "0", num2 = "0", total = "", sign = "";
       // 数字1按钮
        protected void btnone_Click(object sender, EventArgs e)
        {
            total += "1";
            txtDisplay.Text = total;
        }
        // 数字2按钮
        protected void btnTwo_Click(object sender, EventArgs e)
        {
            total += "2";
            txtDisplay.Text = total;
        }
        // 数字3 按钮
        protected void btnThree_Click(object sender, EventArgs e)
        {
            total += "3";
            txtDisplay.Text = total;
        }
        // 加法按钮
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (sign.Length == 1)
            {
                Count();
                num1 = txtDisplay.Text;
                sign = "+";
            }
            else
            {
                num1 = txtDisplay.Text;
                txtDisplay.Text = "";
                total = "";
                sign = "+";
            }
        }
        //减法按钮
        protected void btnSubtract_Click(object sender, EventArgs e)
        {
            if (sign.Length == 1)
            {
                Count();
                num1 = txtDisplay.Text;
                sign = "-";
            }
            else
            {
                num1 = txtDisplay.Text;
                txtDisplay.Text = "";
                total = "";
                sign = "-";
            }
        }
        // 等于按钮
        protected void btnEqual_Click(object sender, EventArgs e)
        {
            Count();
        }
        // 函数定义
        protected void Count()
        {
            num2 = txtDisplay.Text;
            if (num2 == "")
            {
                num2 = "0";
            }
            switch (sign)
            {
                case "+":
                    txtDisplay.Text = (int.Parse(num1) + int.Parse(num2)).ToString();
                    num1 = "0";
                    num2 = "0";
                    total = "";
                    sign = "";
                    break;
                case "-":
                    txtDisplay.Text = (int.Parse(num1) - int.Parse(num2)).ToString();
                    num1 = "0";
                    num2 = "0";
                    total = "";
                    sign = "";
                    break;
            }
        }
测试

在这里插入图片描述 在这里插入图片描述

关注
打赏
1665165634
查看更多评论
立即登录/注册

微信扫码登录

0.2981s