c#实现一个 简单 的 计算器程序 ——重点是 backspace的处理

1、界面   显示文本用了一个textBox  它的 name 是 “textBox1” 第一行的白条一样的东西

2、点击按钮之后显示在textBox1 上 的代码: 以按钮“1” 为例

3、运算  如何解决?

其实 用 DataTable 的 Compute 方法 就能便捷的 把一个  字符串内部的 数学式子 的值 给 求出来了, 学过python的童鞋应该知道 eval()这个函数,就很好理解了。  

这里 包装成了个 setText函数  按 “=” 号的时候 调用一下 ,就会显示结果了

4、重点:backSpace 的处理:

概念 :1  鼠标的位置:textBox1.SelectionStart

            2  鼠标选中字符串的长度: textBox1.SelectionLength

--> 首先 backSpace 不仅可以删除一个字符,鼠标选定一段 字符串 要求也能删除,调用Remove(pos,count) 

当然这种情况是使用上了鼠标                                   backspace 的name 就是 button1_Click

           len确定选中了一段字符串,还是简单的删除一个字符,之后 startLoc不变,但SelectionLength要归零,下次还要用

 

--> 其次,定位鼠标的那个“竖线”要求在需要的时候出现,以便确定删除字符起始、末尾的位置,为什么?看最后的gif好了

始终记着startLoc 的原因是 判断当前有没有 字符可以删除了,前面没有字符就可以不用删除

上面是用到鼠标的情况



下面就是不用鼠标的情况  其实很简单  没有鼠标?假装有不就好了,反正上面两个函数还是可以用的~~~

特性: 当按下按钮之后 textBox1.Text改变,则textBox1的SelectionStart会被置为零!

利用这个特性,可以在 textBox1的SelectionStart==0时 (此时没有鼠标那根竖线了)并且写一段逻辑——不用点击鼠标确定去操作这个textBox 直接默认按backspace 就删除它里面的内容去了 (其实这是我们下意识这么认为的)

当然Text.Length >0 也得有东西删才行, 其实上面的else可以不用,但是 这样做可以更看清楚 那个鼠标的作用

 

用了 Text.Length 确定 SelectionStart的位置 

最后不要忘记   所有的SlectionStart之后,都需要 调用 textBox1.Focus()才能让鼠标那根竖线 显示在 最后,不然会全选~~

最后贴代码:

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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private int startLoc=0;
        private void clearText()
        {
            this.textBox1.Clear();
        }
        private void setText()
        {
            DataTable dt = new DataTable();
            this.textBox1.Text = dt.Compute(this.textBox1.Text, "").ToString();
        }
        private void button18_Click(object sender, EventArgs e)
        {
            setText();
        }
        public void addnum(char i)
        {
            textBox1.Text = textBox1.Text + i.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "1";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "2";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "3";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "4";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "5";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "6";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "7";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "8";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "9";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "0";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += ".";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "/";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "*";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "-";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.textBox1.Text += "+";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clearText();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            startLoc = textBox1.SelectionStart;//这里要注意,改变text内容,此变量立即变为0
            int len = textBox1.SelectionLength;
            if (len > 0)
            {
                textBox1.Text = textBox1.Text.Remove(startLoc , len);
                
                textBox1.SelectionStart = startLoc;
                
                textBox1.SelectionLength = 0;
            }
            else
            {
                if (startLoc > 0)
                {
                    textBox1.Text = textBox1.Text.Remove(startLoc - 1, 1);
                    
                    textBox1.SelectionStart = startLoc - 1;//因为字符串改变后,startLoc会重新变为0;
                    
                }else//这里用来处理鼠标没有点击文本框
                {
                    if (textBox1.Text.Length > 0)
                    {
                        textBox1.SelectionStart = textBox1.Text.Length;
                        startLoc = textBox1.SelectionStart;
                        textBox1.Text = textBox1.Text.Remove(startLoc - 1, 1);
                        textBox1.SelectionStart = textBox1.Text.Length;//删除后还是定位在最后一位
                                                                       //鼠标没有点击textbox时也可以按backspace
                    }
                }
            }
            textBox1.Focus();
        }

        private void button19_Click(object sender, EventArgs e)
        {
            //this.textBox1.Text=this.textBox1.
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Focus();

        }
    }
}

其他没什么 ,就是textBox 和 backSpace的 名字注意一下 分别是 textBox1  和 button1 逻辑应该是没问题的。难点在backspace

点backspace

点 =

用鼠标时:

此时鼠标那根竖线 在  5 前面  4后面

点backspace

竖线还在4后面

还是gif方便:

用到鼠标要选定的情况

不用鼠标选定的情况

码了半小时~~会继续完善

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值