Xây dựng ứng dụng Calculator trong C#


Bạn đã từng sử dụng chương trình Calculator của Windows. Trong bài này chúng ta sẽ xây dựng một chương trình Calculator để tính toán các chức năng đơn giản như cộng, trừ, nhân chia, sin, cos, … Tương tự các bạn có thể làm các chức năng khác. Bài này các bạn sẽ học cách dùng Windows Forms, các Component hữu ích khác và các Control để tạo GUI cho ứng dụng để tăng tính tương tác với người sử dụng. Giao diện đề xuất như ở dưới:

anh1

Bước 1: Tạo một dự án  dạng Windows Forms Application

anh2

Bước 2: Thiết kế giao diện

– Kích trên thanh Toolbox của chương trinh Visual Studio để mở nó. Bạn sẽ nhìn thấy một danh sách các Control như hiển thị ở dưới:

anh3

– Kéo thả một Control Textbox vào form và đặt lại tên của nó trong cưa sổ Properties với thuộc tính Name = txtBox

– Kéo và thả 21 nút dạng Control Button và đổi tên của Control qua thuộc tính Name: button0 thành cmd0, button1 thành cmd1, button2 thành cmd2, button3 thành cmd3,  button4 thành cmd4, button5 thành cmd5, button6 thành cmd6, button7 thành cmd7, button8 thành cmd8, button9 thành cmd9, button10 thành cmdEqual, button11 thành cmdClear, button12 thành cmdAdd, button13 thành cmdSubtract, button14 thành cmdMultiply, button15 thành cmdDivide, button16 thành cmdSquare, button17 thành cmdSqtr, button18 thành cmdCos, button19 thành cmdSin, button20 thành cmdTan.

– Thay đổi nhãn hiển thị trên các button tương ứng với các tên của button như button0 đổi thuộc tính Text = 0, button1  thành 1, button2  thành 2…vv.

Bước 3: Viết code

Để viết code cho các sự kiện tương tác trên các nút, bạn kích đúp trên các nút mà bạn muốn viết code rồi tham khảo phần code phía dưới hoặc copy và paste vào đúng vị trí.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace CWindowGUI
{
    public partial class Calculator : Form
    {
        //global variables
        string sign;
        double val1;
        double val2;
        int trackkeypoint=0;
 
        public Calculator()
        {
            InitializeComponent();
        }
 
        private void cmd0_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text+cmd0.Text;
        }
 
        private void cmd1_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd1.Text;
        }
 
        private void cmd2_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd2.Text;
        }
 
        private void cmd3_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd3.Text;
        }
 
        private void cmd4_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd4.Text;
        }
 
        private void cmd5_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd5.Text;
        }
 
        private void cmd6_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd6.Text;
        }
 
        private void cmd7_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd7.Text;
        }
 
        private void cmd8_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd8.Text;
        }
 
        private void cmd9_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text + cmd9.Text;
        }
 
        private void cmdequal_Click(object sender, EventArgs e)
        {
            val2 =double.Parse(txtBox.Text);
            double result;
            if (sign == "+")
            {
                result = val1 + val2;
                txtBox.Text =result.ToString();
            }
            else if (sign == "-")
            {
                result = val1 - val2;
                txtBox.Text = result.ToString();
            }
            else if (sign == "*")
            {
                result = val1 * val2;
                txtBox.Text = result.ToString();
            }
            else
            {
                result = val1 / val2;
                txtBox.Text = result.ToString();
            }
          
        }
 
        private void cmdClear_Click(object sender, EventArgs e)
        {
            //clear Texts
            txtBox.Text = "";
            val1 = 0;
            val2 = 0;
            sign = "";
        }
 
        private void cmdCos_Click(object sender, EventArgs e)
        {
           double v;
           v = double.Parse(txtBox.Text);
           txtBox.Text=Math.Cos(v).ToString();
        }
 
        private void cmdSin_Click(object sender, EventArgs e)
        {
            double v;
            v = double.Parse(txtBox.Text);
            txtBox.Text = Math.Sin(v).ToString();
        }
 
        private void cmdSquare_Click(object sender, EventArgs e)
        {
            double v;
            v = double.Parse(txtBox.Text);
            txtBox.Text = Math.Pow(v,2).ToString();
        }
 
        private void cmdsqrt_Click(object sender, EventArgs e)
        {
            double v;
            v = double.Parse(txtBox.Text);
            txtBox.Text = Math.Sqrt(v).ToString();
        }
 
        private void cmdTan_Click(object sender, EventArgs e)
        {
            double v;
            v = double.Parse(txtBox.Text);
            txtBox.Text = Math.Tan(v).ToString();
        }
 
        private void cmdSubtract_Click(object sender, EventArgs e)
        {
           sign = "-";
           val1 = double.Parse(txtBox.Text);
           txtBox.Text = "";
        }
 
        private void cmdplus_Click(object sender, EventArgs e)
        {
            sign = "+";
            val1 = double.Parse(txtBox.Text);
            txtBox.Text = "";
        }
 
        private void cmdMultiply_Click(object sender, EventArgs e)
        {
            sign = "*";
            val1 = double.Parse(txtBox.Text);
            txtBox.Text = "";
        }
 
        private void cmdDivide_Click(object sender, EventArgs e)
        {
            sign = "/";
            val1 = double.Parse(txtBox.Text);
            txtBox.Text = "";
        }
 
        private void txtBox_TextChanged(object sender, EventArgs e)
        {
          //Ban viet them code neu can thiet
        }
 
        private void txtBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            int keycode;
            keycode = e.KeyChar;
            //accept only number from key 0 to key 9, key back, and key dot
            if (keycode >= 48 && keycode <= 57 || keycode==8 || keycode==32 || keycode==46) 
            {   //key dot allowed only one time 
                if (keycode == 46) ++trackkeypoint;
                if (trackkeypoint > 1) { e.Handled = true; --trackkeypoint; }
            }
            else e.Handled = true;
        }
 
        private void txtBox_KeyDown(object sender, KeyEventArgs e)
        {
           //Ban viet them code neu can thiet
        }
    }
}


Có thể bạn sẽ thích…

9 phản hồi

  1. Tung viết:

    cho e hỏi trong private void cmdequal_click{} sao chỉ khai báo mỗi val2 =double.Parse(txtBox.Text); thôi ạ mà ko khai báo val1 nữa?

  2. Toán viết:

    các cao nhân cho em hỏi nếu muốn không làm 2 biến mà muốn 3 biến hay nhiều hơn thì cần làm thế nào ạ

  3. Đạt viết:

    mấy vị huynh đài ơi, em đang dùng Visual Studio 2019, tại sao e bị lỗi phần using System.Windows.Forms

  4. dylan viết:

    v = double.Parse(txtBox.Text); sao e dùng bản 2019 bị báo lỗi đoạn này nhỉ

Trả lời

EnglishVietnamese