C# WinForm (Simple Calculator Example)

Keywords: calculator Programming

Original Link: http://www.cnblogs.com/leeice/archive/2012/03/09/2387783.html

I'm impressed to see blogs or articles (about programming) written by many netizens.They show not only ability, but also important help or guidance to those who seek knowledge (and novice birds like me).

Because I'm a beginner (in C#), I can't give you much technical help, but what I want to say is that since we post our discussion on the Internet or our opinions and summaries in one area or in another, the result is to let more people know and understand their opinions.Understanding is also to understand people's confusion; besides technology, programming ideas are also important for programmers, so I hope you prawns can make the program more concise and clear when solving people's confusion.Since I'm writing a program and I'm searching the Internet for questions, take a simple calculator I've written for example:

Take calculator 0-9 for example

Code method one for clicking the 0-9 numeric button:

private void Button(object sender, EventArgs e)
{
     Button btn = sender as Button;
     textBox1.Text += btn.Text;

}

Code method two for clicking the 0-9 numeric button:

 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;
    ........
}

Although the effect is the same, the two ways of writing must give people different feeling!!!That's what I want to say.

Here is the calculator code that I wrote for your reference. If a master wants to find any discomfort or better writing, please give more advice, Thank you!:

namespace Counter
{

    public partial class Form1 : Form
    {
        string _operStr = "";// Operator
        double _dFirst = 0;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        ///Construction button click event
        /// </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;
            textBox1.Text = Convert.ToDouble(Fvalue + Newvalue).ToString();
        }

        /// <summary>
        ///Construct click events for operation operators
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>                                                    
        private void Operator(object sender, EventArgs e)
        {
            _operStr = (sender as Button).Text;// Record Operators, +, -, *, /,%
            //Button bt = sender as Button;
            //_operStr = bt.Text;
            _dFirst = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
        }

        /// <summary>
        ///Click on the equal sign event
        /// </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>
        ///Make the decimal point appear only once
        /// </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>
        ///Backspace
        /// </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>
        ///Empty
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";

        }
        /// <summary>
        ///Percentage Sign
        /// </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();
        }
    }
}

The first time you write a blog, there is basically nothing to say or do. Please correct what you think is wrong or bad. Thank you first!!!

Reprinted at: https://www.cnblogs.com/leeice/archive/2012/03/09/2387783.html

Posted by theironchef on Fri, 02 Aug 2019 20:53:51 -0700