Lập trình C# Winform -Tính Tổng Hiệu Tích Thương

0

Thiết kế giao diện như hình. Khi nhấn chọn vào phép tính nào thì sẽ hiện kết quả của phép tính đó vào ô Kết quả.

Trước khi tính cần kiểm tra dữ liệu nhập phải là số.

Code

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 bai2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        
        private void cong_CheckedChanged(object sender, EventArgs e)
        {
            // Ep kieu sang dang so
            float a = float.Parse(so1.Text);
            float b = float.Parse(so2.Text);
            float tong = a + b;

            if(cong.Checked==true)
            {
                ketqua.Text =tong.ToString() ;
            }
            // Kinh nghiệm: Muốn hiển thị số lên ô textbox(input)--> Biến.ToString() 
            // ToString() phải có ngoặc () ở đằng sau
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void tru_CheckedChanged(object sender, EventArgs e)
        {

            float a = float.Parse(so1.Text);
            float b = float.Parse(so2.Text);
            float hieu = a - b;

            if (tru.Checked == true)
            {
                ketqua.Text = hieu.ToString();
            }
            // Kinh nghiệm: Muốn hiển thị số lên ô textbox(input)--> Biến.ToString() 
            // ToString() phải có ngoặc () ở đằng sau
            //B1 : LẤY DL-->B2 XỬ LÝ-->B3 : IN  RA
        }

        private void nhan_CheckedChanged(object sender, EventArgs e)
        {
            float a = float.Parse(so1.Text);
            float b = float.Parse(so2.Text);
            float tich = a * b;

            if (nhan.Checked == true)
            {
                ketqua.Text = tich.ToString();
            }
        }

        private void chia_CheckedChanged(object sender, EventArgs e)
        {
            // B1: LẤY DL
            float a = float.Parse(so1.Text);
            float b = float.Parse(so2.Text);

            // B2: XỬ LÝ
            float thuong = a / b;
           
            if(chia.Checked==true)
            {
                //B3: IN RA
                ketqua.Text = thuong.ToString();
            }
        }
    }
}
Leave A Reply

Your email address will not be published.