C# WinForm(简易计算器示例)

看到很多网友写的博客或发表文章(关于编程方面的),很是佩服啊!他们展示的不仅是能力,更多的是给那些求知者(还有像我这样的菜鸟级的)提供了重要的帮助或指点。

因为我是一个初级入门者(在C#方面),给不了大家技术上什么太大的帮助,不过我想说的是,既然我们在网络上发表我们的论述或在某一方面或者说某一领域的见解和总结,其结果就是为了让更多的人去了解,去明白自己的见解,也是为了解人之所惑;对于程序员来说除了技术,编程思想也很重要,所以希望各位大虾在解人之惑时,能够把程序更加简洁明了。因为我在写程序时,上网搜索疑问时遇到的一些情况,就拿我写的一个简易计算器举个示例吧:

就拿计算器0-9举个例子吧

单击0-9数字按钮的代码方法一:

1
2
3
4
5
6
private void Button(object sender, EventArgs e)
{
     Button btn = sender as Button;
     textBox1.Text += btn.Text;
 
}

单击0-9数字按钮的代码方法二:

1
2
3
4
5
6
7
8
9
10
private void Button(object sender, EventArgs e)
   textBox1.Text += Button1.Text;
   textBox1.Text += Button2.Text;
   textBox1.Text += Button3.Text;
   textBox1.Text += Button4.Text;
   textBox1.Text += Button5.Text;
   textBox1.Text += Button6.Text;
    ........

虽说效果一样,但这两种写法给人的感觉肯定不一样!!!这就是我想说的。

下面是我写的计算器代码,供大家参考,高手要发现什么不适或更好的写法,请多多指教,Thank you!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace Counter
{
 
    public partial class Form1 : Form
    {
        string _operStr = "";// 操作符
        double _dFirst = 0;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// 构造按钮的单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button(object sender, EventArgs e)
        {
               var Fvalue = textBox1.Text;
            var Newvalue = (sender as Button).Text;
1
2
3
4
5
6
7
8
9
10
    textBox1.Text = Convert.ToDouble(Fvalue + Newvalue).ToString();
}
 
/// <summary>
/// 构造操作运算符的单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>                                                   
private void Operator(object sender, EventArgs e)
{
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
            _operStr = (sender as Button).Text;// 记录操作符,+,-,*,/,%
            //Button bt = sender as Button;
            //_operStr = bt.Text;
            _dFirst = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
        }
 
        /// <summary>
        /// 点击等号事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnEqual_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_operStr)) return;
 
            switch (_operStr)
            {
                case "+":
                    textBox1.Text = (_dFirst + Convert.ToDouble(textBox1.Text)).ToString();
                    break;
                case "-":
                    textBox1.Text = (_dFirst - Convert.ToDouble(textBox1.Text)).ToString();
                    break;
                case"*":
                    textBox1.Text = (_dFirst * Convert.ToDouble(textBox1.Text)).ToString();
                    break;
                case"/":
                    textBox1.Text = (_dFirst / Convert.ToDouble(textBox1.Text)).ToString();
                    break;
            }
        }
 
        /// <summary>
        /// 使小数点只出现一次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDot_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Contains(".")) return;
 
            textBox1.Text += (sender as Button).Text;
 
        }
 
        /// <summary>
        /// 退格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnQuit_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                textBox1.Text = textBox1.Text.Substring(0,                                   textBox1.Text.Length - 1);
            }
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
 
        }
        /// <summary>
        /// 百分号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnPercent_Click(object sender, EventArgs e)
        {
            string s = textBox1.Text;
            double a_Per=Convert.ToDouble(s);
            double b_Per = a_Per / 100.0;
            textBox1.Text = b_Per.ToString();
        }
    }
}

第一次写博客,基本是有啥说啥,大家要觉得有啥说的不对或不好的地方请指正,先谢过了!!!微笑

posted @   LEE ICE  阅读(2081)  评论(2编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示